mcp

package
v0.22.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 9 Imported by: ? 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 serves ai.Tools to MCP clients over streamable HTTP and consumes remote MCP servers as ai.Tools, on the official Go MCP SDK.

Index

Constants

View Source
const (
	// ClientName is the implementation name Connect reports to servers.
	ClientName = "bboxsys-ai"
	// DefaultHTTPTimeout bounds each HTTP exchange with a remote server
	// when ClientOptions.HTTPTimeout is zero.
	DefaultHTTPTimeout = 30 * time.Second
)
View Source
const (
	// DefaultServerName is the implementation name Handler advertises.
	DefaultServerName = "bboxsys"
	// DefaultServerVersion is the implementation version Handler advertises.
	DefaultServerVersion = "1.0.0"
)

Variables

This section is empty.

Functions

func Handler

func Handler(get func(r *http.Request) ([]ai.Tool, error)) http.Handler

Handler serves MCP over streamable HTTP with DefaultServerName and DefaultServerVersion. See HandlerWithInfo.

func HandlerWithInfo

func HandlerWithInfo(name, version string, get func(r *http.Request) ([]ai.Tool, error)) http.Handler

HandlerWithInfo serves MCP over streamable HTTP. get builds the tool set for each request from whatever the route put in the request (the caller identity, for example); an error answers 400 and serves nothing.

The handler is stateless (StreamableHTTPOptions.Stateless): every POST creates a short-lived session over its own request, no Mcp-Session-Id is issued, and GET and DELETE answer 405, so instances behind a load balancer need no session affinity. The SDK connects that session with the request's context, and jsonrpc2 derives every tool handler's context from it with values preserved, so ai.CallerFromContext works inside Execute; the tools are also wrapped with the caller from the request so this does not depend on the SDK's internals. Localhost DNS rebinding protection is off because the endpoint sits behind bearer authentication and is normally reached through a reverse proxy whose Host header is not localhost.

func NewServer

func NewServer(name, version string, tools []ai.Tool) *sdk.Server

NewServer returns an MCP server offering tools. Each tool's input schema is published as-is and its ReadOnly flag becomes the readOnlyHint annotation. A tool whose schema is not a JSON object is skipped, since the SDK refuses it. The server is not connected to any transport.

Types

type Client

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

Client is a session with one remote MCP server.

func Connect

func Connect(ctx context.Context, name, endpoint, bearer string) (*Client, error)

Connect opens a streamable HTTP session with the server at endpoint, sending bearer on every request, with the default options: readOnlyHint is not trusted, so every remote tool is a write. See ConnectWithOptions.

func ConnectWithOptions

func ConnectWithOptions(ctx context.Context, name, endpoint, bearer string, opts ClientOptions) (*Client, error)

ConnectWithOptions opens a streamable HTTP session with the server at endpoint, sending bearer on every request. name prefixes the wrapped tool names ("{name}_") so several servers can be offered together; it is sanitized to the tool name alphabet. Only request and response traffic is used: no standalone SSE stream is opened, so servers that answer GET with 405 work. ctx bounds the handshake; each HTTP exchange is also bounded by opts.HTTPTimeout.

func (*Client) Close

func (c *Client) Close() error

Close ends the session.

func (*Client) Name

func (c *Client) Name() string

Name returns the sanitized server name used as the tool prefix.

func (*Client) Tools

func (c *Client) Tools(ctx context.Context) ([]ai.Tool, error)

Tools lists the server's tools and wraps each as an ai.Tool. A tool is read-only only when the client trusts the server's annotations (ClientOptions.TrustReadOnly) and the server sets readOnlyHint.

type ClientOptions

type ClientOptions struct {
	// TrustReadOnly takes the server's readOnlyHint annotations at their
	// word. Off by default: a remote server decides its own annotations,
	// so a write tool could claim to be read-only and be auto-approved.
	// Untrusted, every remote tool is a write and needs approval. Turn it
	// on only for a server the operator controls.
	TrustReadOnly bool
	// HTTPTimeout bounds each HTTP exchange. Default DefaultHTTPTimeout.
	HTTPTimeout time.Duration
}

ClientOptions configures ConnectWithOptions.

Jump to

Keyboard shortcuts

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