mcp-golang
repository·main·Indexed 22 days ago
https://github.com/metoro-io/mcp-golangAn unofficial implementation of the Model Context Protocol (MCP) in Go. It enables developers to build type-safe MCP servers and clients with minimal boilerplate, utilizing native Go structs for tool arguments and automatic schema generation. The library supports stdio and HTTP transports, including a specialized GinTransport for integration with the Gin web framework, and provides features for tool and prompt discovery, cursor-based pagination, and Claude Desktop configuration.
What's inside mcp-golang
- mcp-golang is an unofficial Go implementation of the Model Context Protocol (MCP). It is a framework designed to build MCP servers that allow AI models to interact with external data and functionality through a standardized protocol. It supports core MCP primitives including tools, resources, and prompts.
Demonstrated MCP Client Features
mainThe client example showcases the following core Model Context Protocol (MCP) capabilities:
Tools
- Listing Tools: Discovering available tools provided by the server.
- Calling Tools: Executing server-side functions. The example demonstrates:
hello: Basic greeting.calculate: Arithmetic operations.time: Current time formatting.
Prompts
- Listing Prompts: Discovering available prompt templates.
- Using Prompts: Executing server-side prompt logic. The example demonstrates:
uppercase: Text conversion to uppercase.reverse: Text reversal.
Create nested navigation groups
mainTo create nested navigation, include an object containing a
groupand apagesarray inside thepagesarray of a parent group. You do not need to include the.mdxextension in page names."navigation": [ { "group": "Getting Started", "pages": [ "quickstart", { "group": "Nested Reference Pages", "pages": ["nested-reference-page"] } ] } ]How pagination works in MCP-Golang
mainMCP-Golang implements cursor-based pagination for
listoperations involving tools, prompts, and resources. This allows servers to return data in manageable chunks.When pagination is enabled, the server follows this lifecycle:
- It limits the number of items returned in a single response to a specified limit.
- If more items exist beyond the current page, the server includes a
nextCursorin the response. - The client can then pass this
cursorvalue as a parameter in subsequent requests to retrieve the next page of results.
Note: The cursor is an opaque string; clients should treat it as a black box and not attempt to parse or manipulate it.
Generate tool schemas automatically
mainYou do not need to manually maintain JSON schemas for your tools.
mcp-golanguses reflection to inspect your handler's argument struct and automatically generates the appropriate MCPinputSchema.To control the schema, use
jsonschematags on your argument struct fields:- Required fields: Use
jsonschema:"required". - Optional fields: Omit the
requiredtag (fields are optional by default). - Descriptions: Use
jsonschema:"description=..."to provide metadata that helps the LLM understand the argument.
- Required fields: Use
Understand the mcp-golang project structure
mainThe repository is organized into the following key packages:
server/: Core server implementationtransport/: Transport layer implementations (e.g.,stdio,SSE)protocol/: MCP protocol implementationexamples/: Example implementationsinternal/: Internal utilities and helpers
How change notifications work in mcp-golang
mainIn
mcp-golang, the server automatically sends notifications to the client whenever the server's capabilities or available assets change. This allows clients (like LLM interfaces) to refresh their context or toolsets dynamically without needing a restart.Notifications are triggered by the following registration and deregistration events:
- Tools: Using
RegisterToolorDeregisterTool. - Prompts: Using
RegisterPromptorDeregisterPrompt. - Resources: Using
RegisterResourceorDeregisterResource.
- Tools: Using
How GinTransport works with Gin
mainTheGinTransportis a specialized transport implementation designed to work with the Gin web framework. It provides aHandler()method which returns a standard Gin handler function. This allows the MCP server to receive and process MCP requests through the Gin router's middleware and routing logic, enabling MCP capabilities to coexist with other RESTful endpoints in the same application.Key features of mcp-golang
mainmcp-golang provides several core capabilities for developers building MCP integrations:
- Batteries Included: Rapidly set up servers with support for tools, resources, and prompts.
- Type Safety: Leverages Go's type system with automatic JSON schema generation derived from Go structs.
- Composable Architecture: Allows developers to use only the necessary components:
transport,protocol, orserver. - Flexible Transports: Includes built-in support for
stdioandsse(Server-Sent Events) transports, while allowing developers to implement custom transports using the rest of the library.
Use HTTP transport for MCP servers and clients
mainThe SDK supports HTTP transport, allowing MCP tools to communicate over HTTP/HTTPS endpoints.
Warning: HTTP transport implementations are stateless. They do not support bidirectional communication features like notifications. If you require persistent connections or notifications, use
stdiotransport instead.How the mcp-golang architecture works
mainThe
mcp-golanglibrary is organized into three functional layers that mirror the Model Context Protocol (MCP) specification. Understanding these layers helps in determining where to implement custom logic versus where to configure communication:- Transport Layer: Manages the raw communication medium (e.g., TCP, HTTP, WebSockets). It is responsible for the bidirectional conversion between underlying transport messages and JSON-RPC messages.
- Protocol Layer: Defines the MCP protocol logic. It consumes JSON-RPC messages and transforms them into structured requests, notifications, and responses. This layer handles JSON-RPC method routing, error handling, and maintains a list of handlers.
- Server Layer: The high-level API used by developers. It integrates the transport and protocol layers to provide a functional server. This layer allows you to register handlers for core MCP features like tools, resources, prompts, and completions.
User Code: Your implementation resides in the handlers passed to the Server Layer. These handlers contain the actual business logic for the tools or resources you expose.
Handle pagination in List methods
mainMethods like
ListToolsandListPromptssupport pagination. To iterate through all results, use a loop that checksNextCursorand passes it back into the next call.var cursor *string for { tools, err := client.ListTools(context.Background(), cursor) if err != nil { log.Fatalf("Failed to list tools: %v", err) } // Process tools... if tools.NextCursor == nil { break // No more pages } cursor = tools.NextCursor }