mcp

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package mcp implements the slice of the Model Context Protocol that the intercom shim needs:

  • Stdio transport (newline-delimited JSON-RPC 2.0)
  • The initialize / notifications/initialized handshake
  • tools/list and tools/call dispatch
  • Public Notify(method, params) for sending arbitrary outbound notifications

The package keeps non-standard outbound notifications explicit because the shim emits notifications/claude/channel events.

This is not a full MCP implementation. It serves one well-defined client (Claude Code) over one transport (stdio).

Index

Constants

View Source
const LatestProtocolVersion = "2025-11-25"

LatestProtocolVersion is the MCP protocol version this implementation targets. We echo the client's version when accepting initialize, falling back to this constant if the client is unspecified.

Variables

This section is empty.

Functions

This section is empty.

Types

type Implementation

type Implementation struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

Implementation describes the server (or client) at a high level. Sent in the initialize result's serverInfo field.

type Options

type Options struct {
	// Instructions is added to the initialize result. Claude Code includes
	// this in the model's system prompt.
	Instructions string

	// Experimental populates capabilities.experimental verbatim. The intercom
	// shim sets this to {"claude/channel": {}}.
	Experimental map[string]any
}

Options configures a Server. All fields are optional.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a stdio-based MCP server. Construct with NewServer, register tools with RegisterTool, then run with Run.

func NewServer

func NewServer(impl Implementation, opts Options) *Server

NewServer constructs a Server. The server does no I/O until Run is called.

func (*Server) Initialized

func (s *Server) Initialized() <-chan struct{}

Initialized returns a channel closed after the client has completed the initialize/initialized handshake. Callers may want to wait on this before emitting their first Notify.

func (*Server) Notify

func (s *Server) Notify(method string, params any) error

Notify sends a JSON-RPC notification with the given method and params. Goroutine-safe. Returns an error only if marshaling or stdout write fails.

func (*Server) RegisterTool

func (s *Server) RegisterTool(t Tool)

RegisterTool adds a tool. Must be called before Run; tool registration is not safe to mutate while the server is serving (and the spec requires sending listChanged, which we don't implement).

func (*Server) Run

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

Run serves MCP over the given reader/writer until in EOFs or ctx is cancelled. It returns nil on a clean EOF, ctx.Err() on cancellation, or a non-nil error on a fatal I/O or protocol failure.

type Tool

type Tool struct {
	Name        string
	Description string
	// InputSchema is a raw JSON object describing the tool's parameters as
	// JSON Schema. Stored raw so callers can author it inline without paying
	// a reflective inference dependency.
	InputSchema json.RawMessage
	Handler     ToolHandler
}

Tool is a single registered MCP tool.

type ToolHandler

type ToolHandler func(ctx context.Context, args json.RawMessage) (ToolResult, error)

ToolHandler implements a registered tool. It is invoked once per tools/call request and may run concurrently with other tool calls.

Return a non-nil error only for protocol-level failures (e.g. malformed arguments that bypass the schema, internal panics). For user-facing errors — "no such peer", "broker disconnected" — return a ToolResult with IsError=true; that surfaces in Claude's context as part of the tool output rather than as an MCP error response.

type ToolResult

type ToolResult struct {
	Text    string
	IsError bool
}

ToolResult is what a tool handler returns: a text payload and whether to flag it as an error to the caller. Maps to MCP's CallToolResult.

Jump to

Keyboard shortcuts

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