mcp

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: AGPL-3.0 Imports: 14 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodeToolID

func EncodeToolID(server, tool string) string

EncodeToolID namespaces the given tool name with a prefix to identify tools injected by this library. Claude Code, for example, prefixes the tools it includes from defined MCP servers with the "mcp__" prefix. We have to namespace the tools we inject to prevent clashes.

We stick to 5 prefix chars ("bmcp_") like "mcp__" since names can only be up to 64 chars:

See: - https://community.openai.com/t/function-call-description-max-length/529902 - https://github.com/anthropics/claude-code/issues/2326

func FilterAllowedTools

func FilterAllowedTools(logger slog.Logger, tools map[string]*Tool, allowlist *regexp.Regexp, denylist *regexp.Regexp) map[string]*Tool

FilterAllowedTools filters tools based on the given allow/denylists. Filtering acts on tool names, and uses tool IDs for tracking. The denylist supersedes the allowlist in the case of any conflicts. If an allowlist is provided, tools must match it to be allowed. If only a denylist is provided, tools are allowed unless explicitly denied.

func GetClientInfo

func GetClientInfo() mcp.Implementation

GetClientInfo returns the MCP client information to use when initializing MCP connections. This provides a consistent way for all proxy implementations to report client information.

Types

type ServerProxier

type ServerProxier interface {
	// Init initializes the proxier, establishing a connection with the upstream server and fetching resources.
	Init(context.Context) error
	// Gracefully shut down connections to the MCP server. Session management will vary per transport.
	// See https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#session-management.
	Shutdown(ctx context.Context) error

	// ListTools lists all known tools.
	ListTools() []*Tool
	// GetTool returns a given tool, if known, or returns nil.
	GetTool(id string) *Tool
	// CallTool invokes an injected MCP tool
	CallTool(ctx context.Context, name string, input any) (*mcp.CallToolResult, error)
}

ServerProxier provides an abstraction to communicate with MCP Servers regardless of their transport. The ServerProxier is expected to, at least, fetch any available MCP tools.

type ServerProxyManager

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

ServerProxyManager can act on behalf of multiple [ServerProxier]s. It aggregates all server resources (currently just tools) across all MCP servers for the purpose of injection into bridged requests and invocation.

func NewServerProxyManager

func NewServerProxyManager(proxiers map[string]ServerProxier) *ServerProxyManager

func (*ServerProxyManager) CallTool

func (s *ServerProxyManager) CallTool(ctx context.Context, name string, input any) (*mcp.CallToolResult, error)

CallTool locates the proxier to which the requested tool is associated and delegates the tool call to it.

func (*ServerProxyManager) GetTool

func (s *ServerProxyManager) GetTool(name string) *Tool

func (*ServerProxyManager) Init

func (s *ServerProxyManager) Init(ctx context.Context) error

Init concurrently initializes all of its [ServerProxier]s.

func (*ServerProxyManager) ListTools

func (s *ServerProxyManager) ListTools() []*Tool

func (*ServerProxyManager) Shutdown

func (s *ServerProxyManager) Shutdown(ctx context.Context) error

Shutdown concurrently shuts down all known proxiers and waits for them *all* to complete.

type StreamableHTTPServerProxy

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

func NewStreamableHTTPServerProxy

func NewStreamableHTTPServerProxy(logger slog.Logger, serverName, serverURL string, headers map[string]string, allowlist, denylist *regexp.Regexp) (*StreamableHTTPServerProxy, error)

func (*StreamableHTTPServerProxy) CallTool

func (p *StreamableHTTPServerProxy) CallTool(ctx context.Context, name string, input any) (*mcp.CallToolResult, error)

func (*StreamableHTTPServerProxy) GetTool

func (p *StreamableHTTPServerProxy) GetTool(name string) *Tool

func (*StreamableHTTPServerProxy) Init

func (*StreamableHTTPServerProxy) ListTools

func (p *StreamableHTTPServerProxy) ListTools() []*Tool

func (*StreamableHTTPServerProxy) Name

func (*StreamableHTTPServerProxy) Shutdown

type Tool

type Tool struct {
	Client ToolCaller

	ID          string
	Name        string
	ServerName  string
	ServerURL   string
	Description string
	Params      map[string]any
	Required    []string
}

func (*Tool) Call

func (t *Tool) Call(ctx context.Context, input any) (*mcp.CallToolResult, error)

type ToolCaller

type ToolCaller interface {
	CallTool(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error)
}

ToolCaller is the narrowest interface which describes the behaviour required from mcp.Client, which will normally be passed into Tool for interaction with an MCP server. TODO: don't expose github.com/mark3labs/mcp-go outside this package.

Jump to

Keyboard shortcuts

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