An example tool that demonstrates basic MCP functionality
The mark3labs/mcp-go library provides a robust Go implementation of the Model Context Protocol (MCP), enabling seamless integration between large language model applications and external data sources and tools. This library allows developers to build MCP servers and clients that can extend AI capabilities through custom tools and services, all while maintaining a standardized communication protocol.
The mark3labs/mcp-go library is a comprehensive Go implementation of the Model Context Protocol (MCP), designed to bridge the gap between LLM applications and external tools or data sources. This library enables developers to create MCP servers that can be integrated with various AI models, providing them with access to custom functionality.
To use mark3labs/mcp-go in your Go project, install it using Go modules:
go get github.com/mark3labs/mcp-go
Creating an MCP server with mark3labs/mcp-go is straightforward:
package main
import (
"context"
"log"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
func main() {
// Create a new MCP server
s := server.NewMCPServer()
// Register tools
s.AddTool(mcp.Tool{
Name: "example_tool",
Description: "An example tool that demonstrates MCP functionality",
Parameters: []mcp.Parameter{
{
Name: "input",
Type: "string",
Description: "Input parameter for the tool",
Required: true,
},
},
Handler: func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResponse, error) {
// Extract arguments
args := req.GetArguments()
input, _ := args["input"].(string)
// Process and return response
return &mcp.CallToolResponse{
Result: "Processed: " + input,
}, nil
},
})
// Start the server
log.Fatal(s.ListenAndServe(":8080"))
}
The library supports session-based tool management, allowing you to provide different tools for different sessions:
// Add a tool for a specific session
sessionID := "unique-session-id"
s.AddSessionTool(sessionID, myTool)
// Remove a tool from a session
s.RemoveSessionTool(sessionID, "tool_name")
// Clear all tools for a session
s.ClearSessionTools(sessionID)
You can also use mark3labs/mcp-go to create clients that connect to MCP servers:
package main
import (
"context"
"fmt"
"log"
"github.com/mark3labs/mcp-go/client"
"github.com/mark3labs/mcp-go/mcp"
)
func main() {
// Create a new MCP client
c, err := client.NewMCPClient("http://localhost:8080")
if err != nil {
log.Fatal(err)
}
// Call a tool on the server
resp, err := c.CallTool(context.Background(), &mcp.CallToolRequest{
Name: "example_tool",
Arguments: map[string]interface{}{
"input": "Hello, MCP!",
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println("Response:", resp.Result)
}
The library provides flexible argument handling with helper functions:
// Using typed arguments
type MyArgs struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Register a typed tool handler
s.AddTool(mcp.Tool{
Name: "typed_tool",
Handler: mcp.NewTypedToolHandlerFunc(func(ctx context.Context, args MyArgs) (*mcp.CallToolResponse, error) {
return &mcp.CallToolResponse{
Result: fmt.Sprintf("Name: %s, Age: %d", args.Name, args.Age),
}, nil
}),
})
// Binding arguments in a handler
func myHandler(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResponse, error) {
var args MyArgs
if err := req.BindArguments(&args); err != nil {
return nil, err
}
// Use typed args
return &mcp.CallToolResponse{
Result: fmt.Sprintf("Name: %s, Age: %d", args.Name, args.Age),
}, nil
}
For more advanced usage and configuration options, refer to the official repository.