Documentation
¶
Overview ¶
Package gomcp provides a Go implementation of the Model Context Protocol (MCP).
Overview ¶
The Model Context Protocol (MCP) is a standardized communication protocol designed to facilitate interaction between applications and Large Language Models (LLMs). This library provides a complete Go implementation of the protocol with support for all specification versions (2024-11-05, 2025-03-26, and draft) with automatic version detection and negotiation.
Core Features ¶
- Full MCP protocol implementation - Client and server components - Multiple transport options (stdio, HTTP, WebSocket, Server-Sent Events) - Automatic protocol version negotiation - Comprehensive type safety - Support for all MCP operations: tools, resources, prompts, and sampling - Flexible configuration options - Process management for external MCP servers - Server configuration file support
Organization ¶
The library is organized into the following main packages:
- github.com/localrivet/gomcp/client: Client implementation for consuming MCP services
- github.com/localrivet/gomcp/server: Server implementation for hosting MCP services
- github.com/localrivet/gomcp/transport: Transport layer implementations
- github.com/localrivet/gomcp/mcp: Core protocol definitions and version handling
Basic Usage ¶
## Client Example
import "github.com/localrivet/gomcp/client"
// Create a new client
c, err := client.NewClient("my-client",
client.WithProtocolVersion("2025-03-26"),
client.WithProtocolNegotiation(true),
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Call a tool on the MCP server
result, err := c.CallTool("say_hello", map[string]interface{}{
"name": "World",
})
## Server Configuration Example
// Connect to an external MCP server defined in a config file
c, err := client.NewClient("",
client.WithServerConfig("mcpservers.json", "task-master-ai"),
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer c.Close() // Will also stop the server process
// The mcpservers.json file defines how to start and connect to the server:
// {
// "mcpServers": {
// "task-master-ai": {
// "command": "npx",
// "args": ["-y", "--package=task-master-ai", "task-master-ai"],
// "env": { "ANTHROPIC_API_KEY": "${ANTHROPIC_API_KEY}" }
// }
// }
// }
## Server Example
import "github.com/localrivet/gomcp/server"
// Create a new server
s := server.NewServer("example-server",
server.WithLogger(logger),
).AsStdio()
// Register a tool
s.Tool("say_hello", "Greet someone", func(ctx *server.Context, args struct {
Name string `json:"name"`
}) (string, error) {
return fmt.Sprintf("Hello, %s!", args.Name), nil
})
// Start the server
s.Run()
Process Management ¶
GOMCP includes robust functionality for managing external MCP server processes:
- Server configuration loading from JSON files
- Automatic process management (start/stop) for external servers
- Environment variable handling and substitution
- Support for different types of servers (NPX-based, Docker, etc.)
- Clean process termination on client close
For more information, see the docs/examples/server-config.md documentation.
Specification Compliance ¶
This library implements the Model Context Protocol as defined at: https://github.com/microsoft/modelcontextprotocol
For detailed documentation, examples, and specifications, see: https://modelcontextprotocol.github.io/
For more examples, see the examples directory in this repository.
Versioning ¶
gomcp follows semantic versioning. The current version is available through the Version constant.
Index ¶
Constants ¶
const Version = "0.1.0"
Version is the current version of the gomcp library
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
This section is empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package client provides the client-side implementation of the MCP protocol.
|
Package client provides the client-side implementation of the MCP protocol. |
|
test
Package test provides test utilities for the client package.
|
Package test provides test utilities for the client package. |
|
test/draft
Package draft provides test utilities specific to the draft protocol version.
|
Package draft provides test utilities specific to the draft protocol version. |
|
examples
|
|
|
grpc
command
Package main provides an example of using gRPC transport for gomcp
|
Package main provides an example of using gRPC transport for gomcp |
|
http
command
Package main provides an example of using the HTTP transport for gomcp
|
Package main provides an example of using the HTTP transport for gomcp |
|
minimal/client
command
|
|
|
minimal/server
command
|
|
|
mqtt
command
Package main provides an example of using the MQTT transport for gomcp
|
Package main provides an example of using the MQTT transport for gomcp |
|
nats
command
Package main provides an example of using the NATS transport for gomcp
|
Package main provides an example of using the NATS transport for gomcp |
|
server
command
|
|
|
server_config
command
Package main demonstrates how to use the server configuration functionality to manage multiple MCP servers from a configuration file.
|
Package main demonstrates how to use the server configuration functionality to manage multiple MCP servers from a configuration file. |
|
sse
command
Package main provides an example of using the SSE (Server-Sent Events) transport for gomcp
|
Package main provides an example of using the SSE (Server-Sent Events) transport for gomcp |
|
stdio
command
Package main provides an example of using stdio transport for gomcp
|
Package main provides an example of using stdio transport for gomcp |
|
udp
command
Package main provides an example of using UDP transport for gomcp
|
Package main provides an example of using UDP transport for gomcp |
|
unix
command
Package main provides an example of using Unix Socket transport for gomcp
|
Package main provides an example of using Unix Socket transport for gomcp |
|
websocket
command
Package main provides an example of using the WebSocket transport for gomcp
|
Package main provides an example of using the WebSocket transport for gomcp |
|
Package mcp contains the core types and interfaces for the Model Context Protocol.
|
Package mcp contains the core types and interfaces for the Model Context Protocol. |
|
draft
Package draft implements the latest draft version of the MCP specification.
|
Package draft implements the latest draft version of the MCP specification. |
|
v20241105
Package v20241105 implements the 2024-11-05 version of the MCP specification.
|
Package v20241105 implements the 2024-11-05 version of the MCP specification. |
|
v20250326
Package v20250326 implements the 2025-03-26 version of the MCP specification.
|
Package v20250326 implements the 2025-03-26 version of the MCP specification. |
|
Package server provides the server-side implementation of the MCP protocol.
|
Package server provides the server-side implementation of the MCP protocol. |
|
test
Package test provides utilities for testing the MCP implementation.
|
Package test provides utilities for testing the MCP implementation. |
|
Package transport provides the transport layer implementations for the MCP protocol.
|
Package transport provides the transport layer implementations for the MCP protocol. |
|
grpc
Package grpc provides a gRPC transport implementation for the MCP protocol.
|
Package grpc provides a gRPC transport implementation for the MCP protocol. |
|
http
Package http provides an HTTP implementation of the MCP transport.
|
Package http provides an HTTP implementation of the MCP transport. |
|
mqtt
Package mqtt provides a MQTT implementation of the MCP transport.
|
Package mqtt provides a MQTT implementation of the MCP transport. |
|
nats
Package nats provides a NATS implementation of the MCP transport.
|
Package nats provides a NATS implementation of the MCP transport. |
|
sse
Package sse provides a Server-Sent Events implementation of the MCP transport.
|
Package sse provides a Server-Sent Events implementation of the MCP transport. |
|
stdio
Package stdio provides a standard I/O implementation of the MCP transport.
|
Package stdio provides a standard I/O implementation of the MCP transport. |
|
udp
Package udp provides a UDP implementation of the MCP transport.
|
Package udp provides a UDP implementation of the MCP transport. |
|
unix
Package unix provides a Unix Domain Socket implementation of the MCP transport.
|
Package unix provides a Unix Domain Socket implementation of the MCP transport. |
|
ws
Package ws provides a WebSocket implementation of the MCP transport.
|
Package ws provides a WebSocket implementation of the MCP transport. |
|
util
|
|
|
conversion
Package conversion provides utilities for converting between types.
|
Package conversion provides utilities for converting between types. |
|
schema
Package schema provides utilities for generating JSON Schema from Go structs.
|
Package schema provides utilities for generating JSON Schema from Go structs. |