Documentation
¶
Overview ¶
Package mcp is a small, dependency-free Model Context Protocol server: tools described by Go functions, served over Streamable HTTP or stdio. Giode uses it to expose a running app to AI agents (see Config.MCP in the root package), and it can be used on its own:
srv := mcp.NewServer("my-app", "1.0.0")
srv.AddTool(mcp.Func("greet", "Greets someone.",
func(ctx context.Context, in struct {
Name string `json:"name" description:"Who to greet"`
}) (any, error) {
return "Hello, " + in.Name, nil
}))
http.Handle("/mcp", srv)
Only the tools capability is implemented; resources and prompts are advertised as empty lists.
Index ¶
- Constants
- func SchemaFor(t reflect.Type) map[string]any
- type Content
- type Result
- type Server
- func (s *Server) AddTool(t Tool)
- func (s *Server) Handle(ctx context.Context, msg []byte) []byte
- func (s *Server) ListenAndServe(ctx context.Context, addr string, ready func(addr string)) error
- func (s *Server) RemoveTool(name string)
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) ServeStdio(ctx context.Context, r io.Reader, w io.Writer) error
- func (s *Server) Tools() []Tool
- type Tool
Constants ¶
const LatestProtocolVersion = "2025-06-18"
LatestProtocolVersion is the newest MCP revision this server speaks.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Content ¶
type Content struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
Data string `json:"data,omitempty"`
MimeType string `json:"mimeType,omitempty"`
}
Content is one item of a tool result.
type Result ¶
type Result struct {
Content []Content `json:"content"`
// IsError reports a tool-level failure the model should see (as
// opposed to a protocol error).
IsError bool `json:"isError,omitempty"`
}
Result is a tool call's result.
type Server ¶
type Server struct {
// Instructions, when set, are sent to clients on initialize to
// explain how to use the server.
Instructions string
// Token, when set, must be sent by HTTP clients as
// "Authorization: Bearer <token>".
Token string
// AllowedOrigins lists browser origins allowed to call the HTTP
// endpoint, besides localhost ones. Requests without an Origin
// header (non-browser clients) are always allowed.
AllowedOrigins []string
// contains filtered or unexported fields
}
Server is an MCP server. Its zero value is not usable; create one with NewServer. A Server is safe for concurrent use.
func (*Server) Handle ¶
Handle processes one JSON-RPC message and returns the response to send, or nil for notifications.
func (*Server) ListenAndServe ¶
ListenAndServe serves the HTTP transport at addr under the path "/mcp" until ctx is canceled. It returns the address actually bound (useful with port 0) through ready, if non-nil, once listening.
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the Streamable HTTP transport: each POST carries one JSON-RPC message and gets its response as application/json. Server-initiated streams (GET) aren't offered, which the specification allows.
Requests from browsers are only accepted from localhost origins and AllowedOrigins, guarding a local server against DNS rebinding; when Token is set, every request must carry it as a bearer token.
func (*Server) ServeStdio ¶
ServeStdio serves newline-delimited JSON-RPC messages from r, writing responses to w, until r is exhausted or ctx is canceled - the stdio transport, for clients that launch the app as a subprocess. Requests are handled concurrently, so a slow tool doesn't block others.
type Tool ¶
type Tool struct {
// Name identifies the tool; letters, digits, '_' and '-'.
Name string
// Description tells the model what the tool does and when to use
// it.
Description string
// InputSchema is the JSON Schema of the arguments object. Nil
// means no arguments.
InputSchema map[string]any
// ReadOnly hints that the tool doesn't modify anything.
ReadOnly bool
// Handler runs the tool with the raw arguments object.
Handler func(ctx context.Context, args json.RawMessage) (*Result, error)
}
Tool is a callable tool.
func Func ¶
Func builds a Tool from a typed function. The input struct's exported fields become the arguments: their JSON names come from json tags, a field is required unless tagged omitempty, and description and enum ("a,b,c") tags document it. The output is returned as text if it's a string, as-is if it's a *Result, and as JSON otherwise.