A simple greeting tool that takes a name parameter and returns a greeting message
The Golang MCP Framework provides a simple and efficient way to create Model Context Protocol servers using Go. It enables developers to build custom tools and integrations that can be used by AI assistants like Claude and other MCP-compatible clients. With just a few lines of Go code, you can create powerful server implementations that extend AI capabilities through custom APIs.
The Golang MCP Framework allows you to build Model Context Protocol (MCP) servers with minimal Go code. MCP servers enable AI assistants to access external tools, data, and services through a standardized protocol.
To get started with the Golang MCP Framework, you'll need to have Go installed on your system. Then you can install the package using:
go get github.com/metoro-io/mcp-golang
Here's a simple example of how to create an MCP server:
package main
import (
"context"
"fmt"
"log"
"net/http"
mcp "github.com/metoro-io/mcp-golang"
)
func main() {
// Create a new MCP server
server := mcp.NewServer()
// Register a handler for a custom tool
server.RegisterPromptHandler("hello", func(ctx context.Context, req *mcp.PromptRequest) (*mcp.PromptResponse, error) {
name := req.Args["name"]
return &mcp.PromptResponse{
Content: fmt.Sprintf("Hello, %s!", name),
}, nil
})
// Start the server
log.Println("Starting MCP server on :8080")
if err := http.ListenAndServe(":8080", server); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
You can provide instructions for AI assistants about how to use your tools:
server := mcp.NewServerWithInstructions("This server provides tools for greeting users.")
In addition to prompt handlers, you can register resource handlers to serve files and other resources:
server.RegisterResourceHandler("get_image", func(ctx context.Context, req *mcp.ResourceRequest) (*mcp.ResourceResponse, error) {
// Return an image or other resource
return &mcp.ResourceResponse{
ContentType: "image/png",
Data: imageData,
}, nil
})
The framework also includes a client for connecting to MCP servers:
client := mcp.NewClient("http://localhost:8080")
resp, err := client.SendPrompt(context.Background(), "hello", map[string]string{"name": "World"})
if err != nil {
log.Fatalf("Error: %v", err)
}
fmt.Println(resp.Content)
For more control over HTTP connections, you can use the transport package:
import "github.com/metoro-io/mcp-golang/transport"
httpTransport := transport.NewHTTPClientTransport("http://localhost:8080")
client := mcp.NewClientWithTransport(httpTransport)
To connect your MCP server to an AI assistant like Claude, you'll need to configure the assistant to use your server. The exact configuration depends on the assistant you're using, but typically involves specifying the server URL and any authentication requirements.
The repository includes several examples in the examples
directory that demonstrate different use cases and features of the framework. These examples are a great way to learn how to build more complex MCP servers.
For more detailed documentation, visit https://mcpgolang.com.