mcp

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package mcp is a minimal Model Context Protocol server over the stdio transport (newline-delimited JSON-RPC 2.0) — enough to expose RunLore tools to MCP clients such as HolmesGPT, kagent, or Claude Desktop. It implements initialize, tools/list, tools/call, and ping; tool handlers return plain text. Deliberately dependency-free (no SDK) to keep the single static binary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SanitizeName added in v0.3.0

func SanitizeName(s string) string

SanitizeName makes a server/tool name safe for the model-facing tool name: lowercase, non-alphanumeric runs collapsed to a single underscore, trimmed.

Types

type Adapter added in v0.3.0

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

Adapter adapts a remote MCP tool to the investigate.Tool contract (satisfied structurally — this package does not import internal/investigate).

func NewTool added in v0.3.0

func NewTool(c *Client, rt RemoteTool) *Adapter

NewTool builds an adapter for one discovered remote tool, namespaced by server. The returned *Adapter satisfies the investigate.Tool contract structurally.

func (*Adapter) Call added in v0.3.0

func (t *Adapter) Call(ctx context.Context, args string) (string, error)

Call delegates to the remote MCP server, passing args as-is.

func (*Adapter) Description added in v0.3.0

func (t *Adapter) Description() string

Description returns the bounded, sanitized description with a provenance prefix.

func (*Adapter) Name added in v0.3.0

func (t *Adapter) Name() string

Name returns the namespaced tool name in the form <server>__<tool>.

func (*Adapter) Schema added in v0.3.0

func (t *Adapter) Schema() string

Schema returns the JSON schema string for the tool's input, defaulting to {"type":"object"} when the remote tool advertises none.

type Client added in v0.3.0

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

Client is a minimal streamable-HTTP MCP client. It speaks JSON-RPC 2.0 over a single POST endpoint, handling both a JSON and an SSE response, and carries the server's session id (if any) across requests. All HTTP goes through httpx.SecureClient so the SSRF guard and cross-host key-stripping apply.

func NewClient added in v0.3.0

func NewClient(name, url, apiKey string, headers map[string]string, log *slog.Logger) *Client

NewClient builds an MCP client for a server. A nil logger discards logs.

func (*Client) CallTool added in v0.3.0

func (c *Client) CallTool(ctx context.Context, name string, args json.RawMessage) (string, error)

CallTool invokes a remote tool and returns its concatenated text content. An MCP tool error (isError) or a JSON-RPC error becomes a Go error.

tools/call is NOT retried (attempts=1): remote MCP tools may have side effects, and retrying a side-effecting call on a transient 5xx could double-invoke it. A 5xx here becomes an error that the investigation loop surfaces as a tool error; the model may choose to call again explicitly.

func (*Client) Initialize added in v0.3.0

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

Initialize performs the MCP handshake: initialize (capturing any session id) then the notifications/initialized notification. Retries up to 3 times on transient failures.

func (*Client) ListTools added in v0.3.0

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

ListTools returns the server's advertised tools. Retries up to 3 times on transient failures.

func (*Client) Name added in v0.3.0

func (c *Client) Name() string

Name returns the human-readable name of this MCP server connection.

type RemoteTool added in v0.3.0

type RemoteTool struct {
	Name        string          `json:"name"`
	Description string          `json:"description"`
	InputSchema json.RawMessage `json:"inputSchema"`
}

RemoteTool is a tool advertised by an MCP server's tools/list.

type Server

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

Server is a minimal MCP server. Construct with NewServer, AddTool, then Serve.

func NewServer

func NewServer(name, version string, log *slog.Logger) *Server

NewServer creates a server advertising name/version. A nil logger discards logs (logs MUST NOT go to the stdout MCP channel — pass a stderr logger).

func (*Server) AddTool

func (s *Server) AddTool(t Tool)

AddTool registers (or replaces) a tool, preserving first-seen order for tools/list.

func (*Server) Serve

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

Serve reads newline-delimited JSON-RPC requests from in and writes one response line per request to out, until in is exhausted or ctx is cancelled. Notifications (no id) get no response. json.Encoder writes compact, single-line JSON + a newline — exactly the stdio framing MCP expects.

type Tool

type Tool struct {
	Name        string
	Description string
	InputSchema json.RawMessage
	Handler     func(ctx context.Context, args json.RawMessage) (string, error)
}

Tool is an MCP tool: a name, a description, a JSON-Schema for its arguments, and a handler returning text (an error is surfaced to the client as an MCP tool error).

Jump to

Keyboard shortcuts

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