Help us shape the future of Go! Take the 2026 Go Developer Survey

mcp

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package mcp implements a client for the Model Context Protocol (MCP). It connects to MCP servers (via stdio or HTTP), discovers available tools, and dispatches tool calls. Tool definitions are exposed to agents as OpenAI function definitions injected into every chat completion request.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthHeaderFile

type AuthHeaderFile struct {
	HeaderName string
	FilePath   string
	Prefix     string // prepended to file contents (e.g. "Bearer ")
}

AuthHeaderFile maps an HTTP header to a file containing its secret value.

type Client

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

Client manages connections to one or more MCP servers and provides a unified tool discovery and invocation interface.

func New

func New(ctx context.Context, configs []ServerConfig) *Client

New creates an MCP client and connects to all configured servers. Servers that fail to connect are skipped with a warning.

func (*Client) Call

func (c *Client) Call(ctx context.Context, toolName, arguments string) (string, error)

Call invokes a tool on the appropriate MCP server and returns the result as a JSON string.

func (*Client) Close

func (c *Client) Close()

Close disconnects from all MCP servers.

func (*Client) FetchResource

func (c *Client) FetchResource(ctx context.Context, serverName, uri string) (string, error)

FetchResource fetches a ui:// resource from the named MCP server via resources/read and returns the text content (typically HTML).

func (*Client) Tools

func (c *Client) Tools() []Tool

Tools returns all tools discovered from connected MCP servers.

type EnvFileMapping

type EnvFileMapping struct {
	Name     string
	FilePath string
}

EnvFileMapping maps an env var name to a file containing its secret value.

type OAuthConfig

type OAuthConfig struct {
	// CredentialsDir holds the OAuth client credentials as files (client_id,
	// client_secret, redirect_uri, refresh_token).
	CredentialsDir string
	// Scopes overrides the server's scopes_supported; if empty, discovery scopes win.
	Scopes []string
}

OAuthConfig drives OAuth 2.0 token acquisition for an HTTP/SSE MCP server. The model-router performs the exchange itself and holds the access token in memory, so NO access-token Secret is required — only the credentials Secret is mounted as files at CredentialsDir (client_id, client_secret, redirect_uri, refresh_token). This is what makes OAuth-only MCP servers (e.g. Slack, which rejects static bearer tokens with -32001 invalid_token) work without a static bearerSecret.

type ServerConfig

type ServerConfig struct {
	// Name is the Tool CRD name this server corresponds to.
	Name      string
	Transport Transport
	// URL is used for http/sse transports.
	URL string
	// Cmd and Args are used for stdio transport.
	Cmd  string
	Args []string
	Env  []string
	// EnvFiles maps env var names to files containing secret values.
	// Read at stdio subprocess startup.
	EnvFiles []EnvFileMapping
	// AuthHeaderFiles maps HTTP headers to files containing secret values.
	// Read at HTTP/SSE connection time and injected into every request.
	AuthHeaderFiles []AuthHeaderFile
	// OAuth, when set, makes the client perform the OAuth 2.0 exchange (in-memory
	// bearer) instead of using a static authHeaderFile. Only for http/sse transports.
	OAuth *OAuthConfig
	// AllowApps enables MCP App iframe rendering for tools from this server.
	AllowApps bool
	// IncludePatterns and ExcludePatterns filter the tool names the model-router
	// exposes to the LLM after discovering them via tools/list. When IncludePatterns
	// is non-empty, only tools matching at least one pattern are kept. ExcludePatterns
	// always wins. Empty (both unset) means no filtering — all discovered tools are
	// exposed (backward compatible).
	IncludePatterns []string
	ExcludePatterns []string
}

ServerConfig describes a running MCP server to connect to.

type Tool

type Tool struct {
	// ServerName is the ServerConfig.Name that exposes this tool.
	ServerName  string
	Name        string
	Description string
	InputSchema json.RawMessage
	// AppResourceURI is the ui:// resource URI from _meta.ui.resourceUri, if present.
	AppResourceURI string
	// AllowApps is propagated from the server's ServerConfig.AllowApps.
	AllowApps bool
}

Tool is a tool discovered from an MCP server.

type Transport

type Transport string

Transport is the MCP connection transport type.

const (
	TransportStdio Transport = "stdio"
	TransportHTTP  Transport = "http"
	TransportSSE   Transport = "sse"
)

Jump to

Keyboard shortcuts

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