mcp

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package mcp bridges Model Context Protocol clients to tool definitions (S13). Transport abstracts JSON-RPC over stdio, SSE, or other backends. Client initializes the MCP connection, lists tools, and invokes them. Bridge maps server tools to pkg/tools.Tool with the validated M48 schema subset and an execution wrapper.

Public MCP names match mcp-client/tools: stable identity is (server, raw) and the model-facing name is mcp__<server>__<raw>. Sanitization or truncation to the 64-character [A-Za-z0-9_-] limit appends an identity hash to prevent collisions.

Index

Constants

View Source
const MaxPublicNameLength = 64

MaxPublicNameLength limits function-name length.

Variables

This section is empty.

Functions

func MapSchema

func MapSchema(m map[string]any) (*tools.JsonSchemaNode, error)

MapSchema converts JSON Schema maps to the validated M48 JsonSchemaNode subset. Only type, properties, required, items, enum, description, const, and default are mapped. Unsupported keywords such as anyOf fail closed.

func PublicToolName

func PublicToolName(serverName, rawName string) string

PublicToolName deterministically derives a public name from server and raw names.

Types

type Bridge

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

Bridge maps MCP server tools to pkg/tools.Tool.

func NewBridge

func NewBridge(client *Client) *Bridge

NewBridge creates a tool bridge.

func (*Bridge) ToTool

func (b *Bridge) ToTool(schema MCPSchema) (*tools.Tool, error)

ToTool converts a schema to a tool whose Execute wraps CallTool.

func (*Bridge) ToTools

func (b *Bridge) ToTools(schemas []MCPSchema) ([]*tools.Tool, error)

ToolRegistry registers listed tools as *tools.Tool values.

type Client

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

Client initializes MCP, lists tools, and invokes tools.

func NewClient

func NewClient(t Transport) *Client

NewClient creates an MCP client.

func (*Client) CallTool

func (c *Client) CallTool(ctx context.Context, name string, input map[string]any) (string, error)

CallTool invokes an MCP tool and returns text output.

func (*Client) Initialize

func (c *Client) Initialize(ctx context.Context) error

Initialize performs the MCP initialization handshake.

func (*Client) ListTools

func (c *Client) ListTools(ctx context.Context) ([]MCPSchema, error)

ListTools returns all tools exposed by the MCP server.

type MCPSchema

type MCPSchema struct {
	Name        string         `json:"name"`
	Description string         `json:"description,omitempty"`
	InputSchema map[string]any `json:"inputSchema"` // JSON Schema in object form.
}

MCPSchema describes a tool exposed by an MCP server.

type Request

type Request struct {
	ID     uint64         `json:"id"`
	Method string         `json:"method"`
	Params map[string]any `json:"params,omitempty"`
}

Request is a JSON-RPC request.

type Response

type Response struct {
	ID     uint64          `json:"id"`
	Result json.RawMessage `json:"result,omitempty"`
	Error  json.RawMessage `json:"error,omitempty"`
}

Response is a JSON-RPC response preserving the raw result.

type Transport

type Transport interface {
	// RoundTrip sends a request and returns its matching response.
	RoundTrip(ctx context.Context, req Request) (Response, error)
}

Transport implements MCP JSON-RPC round trips over stdio, SSE, or other transports.

Jump to

Keyboard shortcuts

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