mcp

command
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 18 Imported by: 0 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.

README

mcp: using another program's tools over MCP

The Model Context Protocol lets an agent borrow tools from other programs: an MCP server publishes tools, and a client connects, lists them and calls them. AgentKit's client, mcp.Pool, turns every connected server's tools into ordinary core.Tools that the agent runs like its own. This example starts a small "docs" MCP server inside the process (built on the official Go SDK), connects to it, checks the tools work, and hands them to a model. The MCP part needs nothing; the model part needs a provider and its credential.

(To go the other way and serve AgentKit's tools to an MCP client, see harness.)

Run it

export ANTHROPIC_API_KEY=sk-ant-...
go run ./examples/mcp "which topics do you know, and what do you say about qualified names?"

# also spawn and connect a real stdio MCP server
go run ./examples/mcp --external "npx -y @modelcontextprotocol/server-github"
Flag Meaning
--external "<cmd> <args>" Also spawn and connect a stdio MCP server, named github, passing it GITHUB_PERSONAL_ACCESS_TOKEN=${GITHUB_TOKEN}.

Everything after the flags is the prompt.

AGENTKIT_PROVIDER (anthropic, openrouter or ollama), AGENTKIT_MODEL (a tier or a model id, default STANDARD) and AGENTKIT_EFFORT choose the model. Only the examples read these variables. Each provider reads its own credential: ANTHROPIC_API_KEY (or another Anthropic credential), OPENROUTER_API_KEY, or OLLAMA_HOST. See choosing a provider.

The MCP half runs before any model call, so even without a credential you see it work:

connected servers: [docs]
tools discovered over MCP: [docs__list_topics docs__search_docs]
direct call to docs__list_topics: ok=true {"content":[{"type":"text","text":"environment, pipe transport, protocol version, qualified names"}],…
shadowing refused: mcp: tool name collides with an existing tool: server "helper" exposes "word_count" as "word_count", which is already a native tool
error: anthropic: missing credentials: set ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN or ANTHROPIC_OAUTH_TOKEN, …

With a credential, the last line is replaced by the streamed answer, a [calling …] / [<tool>: ok, <n>ms] pair per tool call, and a summary line on stderr.

What it shows

  • MCP tools get qualified names, <server>__<tool>. Write allowlists and guards against the qualified name: a rule written against search_docs never matches, so it fails open.
  • pool.NativeTools, set before connecting, makes the pool refuse a server tool that would take the name of one of your own tools.
  • Calling one adapted tool directly checks the MCP side before you pay for a model turn.
  • A spawned server gets a reduced environment, not os.Environ(); a credential travels as a ${VAR} reference resolved at spawn, and an unset one stops the spawn with *mcp.UnresolvedVariableError.

The code that matters

opts := mcp.ConnectionOptions{
	ClientInfo: mcp.Implementation{Name: "my-app", Version: "0.1.0"},
	Limits:     wire.Defaults(),
}
pool := mcp.NewPool(opts)
defer pool.Close()
pool.NativeTools = []string{"word_count"} // your own tools' names

_, err := pool.Connect(ctx, mcp.ServerConfig{
	Name:    "github",
	Command: "npx",
	Args:    []string{"-y", "@modelcontextprotocol/server-github"},
	Env:     map[string]string{"GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN}"},
	Timeout: 30 * time.Second,
}, []string{"PATH=" + os.Getenv("PATH"), "HOME=" + os.Getenv("HOME")}, os.Getenv)

mcpTools, err := pool.Tools(ctx, native) // github__… core.Tools
all := append(native, mcpTools...)
agent, err := agentkit.New(agentkit.Config{
	Provider: provider, Model: model, Effort: effort,
	Toolset:  agentkit.Toolset{Tools: all, Policy: core.ToolPolicy{ToolNames: toolNames(all)}},
	MaxTurns: 8, MaxCostUSD: 1.00,
})

A non-nil ToolNames is an allowlist over the whole set, so it names your own tools as well as the MCP ones. For an in-process server, the example joins mcp.Connect and the SDK server with mcp.NewPipeTransport and adds the connection with pool.Add.

Further reading

Documentation

Overview

Command mcp is AgentKit as a Model Context Protocol CLIENT: it borrows another program's tools and hands them to a model.

export ANTHROPIC_API_KEY=sk-ant-...
go run ./examples/mcp "which topics do you know, and what do you say about qualified names?"
go run ./examples/mcp --external "npx -y @modelcontextprotocol/server-github"

--external takes any stdio MCP server.

AGENTKIT_PROVIDER, AGENTKIT_MODEL and AGENTKIT_EFFORT choose what runs the model: the provider (anthropic, the default, openrouter or ollama), a tier (SIMPLE, STANDARD, the default, or ADVANCED) or the provider's own model id, and the effort (low to max; the tier's when empty). One run on each provider:

ANTHROPIC_API_KEY=sk-ant-... go run ./examples/mcp "which topics do you know?"
AGENTKIT_PROVIDER=openrouter OPENROUTER_API_KEY=sk-or-... go run ./examples/mcp "which topics do you know?"
AGENTKIT_PROVIDER=ollama AGENTKIT_MODEL=SIMPLE AGENTKIT_EFFORT=high go run ./examples/mcp "which topics do you know?"

It needs nothing installed. It starts an MCP server inside this process, built directly on the official SDK, and talks to it over a pipe with the shipped client — so what runs is the real qualified-name path and not a stand-in that merely has two underscores in its name. AgentKit ships no server of its own.

The protocol is the official Go SDK's (github.com/modelcontextprotocol/go-sdk), which negotiates: it speaks 2026-07-28 to a server that has migrated and falls back to the initialize handshake of an earlier revision for one that has not.

See examples/README.md for the full environment-variable table.

Jump to

Keyboard shortcuts

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