mcp

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mcp is a small, dependency-light Model Context Protocol server: the stdio transport (newline-delimited JSON-RPC 2.0), tools, and read-only resources. It knows nothing about idea-harness; internal/cli supplies the tools and resources and wires them to the daemon's REST API.

Why in-house rather than an SDK: the surface this binary needs is one transport and a handful of methods (server/discover, initialize, ping, tools/list, tools/call, resources/list, resources/templates/list, resources/read). That is a few hundred lines with no new module, versus a dependency whose tracking of the protocol's 2026-07-28 stateless revision we would have to verify on every bump. The input-schema validator is the JSON Schema 2020-12 implementation the module already ships for config validation, so no new code path validates untrusted input.

Protocol eras

The server is dual-era, as the 2026-07-28 revision's compatibility section describes:

  • A request whose params._meta carries io.modelcontextprotocol/protocolVersion is "modern": it is served statelessly, its version is checked against ModernVersions, and a version this server does not speak is refused with UnsupportedProtocolVersion (-32022) naming the versions it does.
  • An `initialize` request is "legacy": the handshake answers with a negotiated LegacyVersions entry, and later requests without modern _meta are served under it. MCP hosts shipping today (Claude Code, Cursor, Codex) still open with `initialize`, so refusing it would make the server unusable from exactly the clients it exists for.

Every result carries resultType "complete": modern clients require it and legacy clients ignore unknown fields.

Errors

The two mechanisms the spec distinguishes are kept apart deliberately:

  • JSON-RPC errors for things the caller's software got wrong -- a parse failure, an unknown method, an unknown (or disabled) tool, arguments that are not an object, an unknown resource.
  • Tool results with isError: true for everything a model can act on -- arguments that fail the tool's input schema, and any error the tool itself returns (a daemon 404/409, "no project registered here"). Those reach the model, which is the point: "change set CHG-9 does not exist" is something it can fix; a protocol error usually is not shown to it.

Index

Constants

This section is empty.

Variables

View Source
var ErrResourceNotFound = errors.New("resource not found")

ErrResourceNotFound is returned (wrapped) by ReadResource for a URI that names nothing. It maps to -32602, which the 2026-07-28 revision mandates in place of the earlier -32002.

View Source
var LegacyVersions = []string{"2025-11-25", "2025-06-18", "2025-03-26", "2024-11-05"}

LegacyVersions are the initialize-handshake revisions served, newest first. Older ones are accepted because the subset used here -- tools with inputSchema, text content, resources -- has not changed shape since 2024-11-05; newer fields (annotations, title, structuredContent) are additive and an older client ignores them.

View Source
var ModernVersions = []string{"2026-07-28"}

ModernVersions are the per-request-metadata protocol revisions served.

Functions

This section is empty.

Types

type Annotations

type Annotations struct {
	Title           string `json:"title,omitempty"`
	ReadOnlyHint    bool   `json:"readOnlyHint"`
	DestructiveHint bool   `json:"destructiveHint"`
	IdempotentHint  bool   `json:"idempotentHint"`
	OpenWorldHint   bool   `json:"openWorldHint"`
}

Annotations are the spec's ToolAnnotations. Every hint is always emitted rather than omitted when false: the spec's defaults for an absent hint are the pessimistic ones (destructiveHint true, openWorldHint true), so omitting a false destructiveHint would tell a client the opposite of what was meant.

type Handler

type Handler func(ctx context.Context, args json.RawMessage) (Result, error)

Handler runs a tool. args is the already schema-validated arguments object. A returned error becomes an isError result whose text is the error message.

type Implementation

type Implementation struct {
	Name    string `json:"name"`
	Title   string `json:"title,omitempty"`
	Version string `json:"version"`
}

Implementation identifies the server (serverInfo).

type Resource

type Resource struct {
	URI         string `json:"uri"`
	Name        string `json:"name"`
	Title       string `json:"title,omitempty"`
	Description string `json:"description,omitempty"`
	MimeType    string `json:"mimeType,omitempty"`
}

Resource is one concrete readable resource.

type ResourceContents

type ResourceContents struct {
	URI      string `json:"uri"`
	MimeType string `json:"mimeType,omitempty"`
	Text     string `json:"text"`
}

ResourceContents is one item of a resources/read answer.

type ResourceTemplate

type ResourceTemplate struct {
	URITemplate string `json:"uriTemplate"`
	Name        string `json:"name"`
	Title       string `json:"title,omitempty"`
	Description string `json:"description,omitempty"`
	MimeType    string `json:"mimeType,omitempty"`
}

ResourceTemplate is a parameterized resource (RFC 6570 URI template).

type Result

type Result struct {
	// Text is the concise, human-and-model-readable answer.
	Text string
	// Structured is the machine form, sent as structuredContent. It must
	// marshal to a JSON object: the 2025-06-18 and 2025-11-25 revisions
	// require an object there, and a list is wrapped ({"items": [...]}) by the
	// caller so every era reads the same shape. Nil sends none.
	Structured any
}

Result is what a tool handler returns on success.

type Server

type Server struct {
	Info         Implementation
	Instructions string
	Tools        []Tool

	// Withheld names tools that exist but were not enabled by this server's
	// configuration, mapped to how to enable them. A call to one is still an
	// unknown tool (a JSON-RPC error, since it is absent from tools/list), but
	// the message says why instead of implying a typo.
	Withheld map[string]string

	ResourceTemplates []ResourceTemplate
	ListResources     func(ctx context.Context) ([]Resource, error)
	ReadResource      func(ctx context.Context, uri string) ([]ResourceContents, error)

	// Logf receives diagnostics. stdout is the protocol channel, so nothing
	// else may ever be written there; this is normally stderr.
	Logf func(format string, args ...any)
	// contains filtered or unexported fields
}

Server is a configured MCP server. The zero value is not usable; fill Info and Tools at least.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, in io.Reader, out io.Writer) error

Serve reads newline-delimited JSON-RPC messages from in and writes responses to out until in reaches EOF (the stdio transport's shutdown signal) or ctx is cancelled.

Requests are handled one at a time, in order. Every tool here is one bounded HTTP round trip to a local daemon -- long work (an apply, a review rerun) returns an operation handle rather than waiting -- so concurrency would buy nothing and would make the daemon auto-start racy.

type Tool

type Tool struct {
	Name        string
	Title       string
	Description string
	// InputSchema is a JSON Schema (2020-12) object for the arguments.
	InputSchema map[string]any
	Annotations Annotations
	Handler     Handler
}

Tool is one callable tool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL