Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Mcp Tool Integration?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/tylerjrbuell-mcp-tool-integration)
---
name: mcp-tool-integration
description: Connect agents to MCP servers using stdio or HTTP transport, with automatic Docker lifecycle management and transport auto-detection.
compatibility: Reactive Agents TypeScript projects using @reactive-agents/*
metadata:
author: reactive-agents
version: "2.0"
tier: "capability"
---
# MCP Tool Integration
## Agent objective
Produce a builder with MCP servers correctly configured — right transport, Docker lifecycle, and auth — so the agent can discover and call MCP tools transparently.
## When to load this skill
- Adding external tools via MCP (GitHub, filesystem, databases, web APIs)
- Connecting to Docker-hosted MCP servers
- Debugging MCP connection or tool discovery failures
- Building agents that use context7, GitHub MCP, or custom MCP servers
Not for the other direction. To let an MCP host (Claude Code, Cursor, Codex) call the agent as a tool, use `agent.serveMCP()` (streamable HTTP, default `http://127.0.0.1:3001/mcp`, one tool `run_agent` with `{ input: string }`) or `rax serve --agent <file> --protocol mcp --transport stdio` (add `--trust-file` when the file starts stdio MCP servers, calls remote agents (`agentTools[].remoteUrl`), sets `tools.terminal` or reads env values: the host gives `rax` no terminal to confirm, so it exits 2 otherwise). Experimental. Remote auth: a static `token`, or OAuth 2.1 resource-server mode from code with `agent.serveMCP({ oauth: { resource, authorizationServers, jwksUri } })` (not in `rax serve`, not on stdio). Both transports take `maxConcurrentRuns` (default 4), `runTimeoutMs` (wall-clock budget per run, default 300000, max 2147483647; a run past it gets the tool error `Agent run timed out`), `responseFormat` (`"text"` default, or `"json"`: the text block carries the JSON of `structuredContent`) and `onError` (receives a failed run's error). `rax serve --protocol mcp` has `--response-format <text|json>` and `--run-timeout-ms <ms>` for the last two (exit 2 with `--protocol a2a`). A successful call returns `structuredContent` (`answer` and `runId` by default); the caller adds `receipt`, `tool_calls` and `unknowns` with the `include` argument, or overrides the format with `format`. The server exposes an `agent://card` resource (name, description, version, tool, limits). Opt-in `agent.serveMCP({ steering: true })` also lists `steer_run` (`{ runId, text }`): a `run_agent` call with a `runId` argument runs as a streamed run under that id so `steer_run` can reach it; a call without one cannot be steered. It reaches only runs this server started; any other id answers `no-live-run`, and a `runId` live elsewhere in the process is refused as `Invalid runId.`. `runId` must match the `run_agent` pattern and `text` is at most 4000 characters (else `InvalidParams`; 8 queued per run). The runtime adds no authentication for it, so enable it only behind `token`/`oauth` or a trusted stdio host: over HTTP without them `serveMCP({ steering: true })` throws `McpServerConfigError` unless `allowUnauthenticatedSteering: true` (`rax serve --steering` needs `RA_MCP_TOKEN` or `--allow-unauthenticated-steering`). Steering works on the reactive kernel only. A failed run reaches the host only as `Agent run failed`; a host cancel stops the run (not on `.withDurableRuns()` agents). See https://docs.reactiveagents.dev/guides/serve-over-mcp/.
## Implementation baseline
```ts
import { ReactiveAgents } from "@reactive-agents/runtime";
const agent = await ReactiveAgents.create()
.withProvider("anthropic")
.withReasoning({ defaultStrategy: "adaptive" })
.withMCP([
{
name: "github",
command: "docker",
args: [
"run", "--rm", "-i",
"-e", "GITHUB_PERSONAL_ACCESS_TOKEN",
"ghcr.io/github/github-mcp-server",
],
env: { GITHUB_PERSONAL_ACCESS_TOKEN: process.env.GITHUB_TOKEN ?? "" },
},
{
name: "filesystem",
command: "docker",
args: [
"run", "--rm", "-i",
"-v", `${process.cwd()}:/workspace`,
"mcp/filesystem", "/workspace",
],
// transport auto-inferred as "stdio" (command present, no endpoint)
},
])
.build();
```
`.withMCP()` implicitly enables the tools layer — `.withTools()` is not required before it.
## Key patterns
### Two Docker MCP patterns
**Pattern A — stdio MCP** (GitHub MCP, filesystem MCP): reads JSON-RPC from stdin
```ts
.withMCP({
name: "github",
command: "docker",
args: ["run", "--rm", "-i", "ghcr.io/github/github-mcp-server"],
env: { GITHUB_PERSONAL_ACCESS_TOKEN: process.env.GITHUB_TOKEN ?? "" },
// transport: inferred as "stdio" — "-i" keeps stdin open for JSON-RPC
})
```
**Pattern B — HTTP-only MCP** (context7, etc.): starts HTTP server, ignores stdin
```ts
.withMCP({
name: "context7",
command: "docker",
args: ["run", "--rm", "-p", "3000:3000", "ghcr.io/upstash/context7-mcp"],
// Framework auto-detects HTTP URL printed to stderr → switches to port-mapped HTTP
// Do NOT hardcode transport here — let auto-detection handle it
})
```
### Transport auto-detection
When a stdio Docker container prints an HTTP URL to stderr, the MCP client races the stdio connection against HTTP URL detection. HTTP wins → client switches to port-mapped HTTP mode automatically.
Two container phases are created:
- `rax-probe-<name>-<pid>` — initial stdio probe attempt
- `rax-mcp-<name>-<pid>` — port-mapped HTTP managed container (if HTTP detected)
PID-based naming prevents conflicts when multiple agents run concurrently.
### Transport field (optional — usually omit)
```ts
// Auto-inferred rules:
// command present, no endpoint → "stdio"
// endpoint ends with /mcp → "streamable-http"
// endpoint (other path) → "sse"
// Only set transport explicitly when overriding detection:
.withMCP({ name: "my-server", endpoint: "http://localhost:3000/api", transport: "sse" })
```
### Non-Docker MCP (local process or remote HTTP)
```ts
// Local process
.withMCP({ name: "my-mcp", command: "node", args: ["./my-mcp-server.js"] })
// npx
.withMCP({ name: "fs", command: "npx", args: ["-y", "@modelcontextprotocol/server-filesystem", "/workspace"] })
// Remote Streamable HTTP
.withMCP({ name: "remote", endpoint: "https://mcp.example.com/mcp" })
// Remote SSE with auth header
.withMCP({ name: "secure", endpoint: "https://mcp.example.com/sse", headers: { Authorization: `Bearer ${process.env.API_KEY}` } })
```
### Large MCP catalogs: the schema budget
MCP servers can bring dozens of tools, and their schemas dominate each request. `disclosure: "auto"` (the default on local and mid models; large and frontier models default to `"discover"`, so set `"auto"` there to use the budget) fits them into `schemaBudget` (4,000 tokens per iteration by default): the agent's own tools and any tool the run needs ship full, relevant MCP tools ship compact, and the rest become one-line index entries the model can still call by name. Pin a tool the task always needs with `focusedTools`:
```ts
.withMCP({ name: "notion", endpoint: "https://mcp.notion.com/mcp" })
.withTools({ focusedTools: ["notion/notion-create-pages"], schemaBudget: 6000 })
```
`disclosure: "all"` ships every MCP schema in full (no budget); `disclosure: "discover"` hides them until the model calls `discover-tools`.
### Secrets in exported configs
`builder.toConfig()` never returns MCP secrets: `env` and `headers` values, secret stdio args (`--api-key X`, `--token=X`, `sk-`/`ghp_`-style keys) and args that repeat a secret read `[redacted:NAME]`; endpoints lose userinfo and credential query parameters; the OAuth `auth` object and `tokenStore` are dropped (`auth: false` stays). Supply secrets again to build from such a config. `agentConfigToSource` prints markers as `process.env.NAME`, and `agentConfigToAgentSpec` writes them as env references.
## Container lifecycle
The framework owns container lifecycle:
- `docker run` starts the container when the agent is built
- On agent dispose, `docker rm -f <containerName>` stops it
**Critical:** `docker rm -f` is the only reliable way to stop MCP Docker containers. Killing the `docker run` process leaves the container alive in the Docker daemon.
## MCPServerConfig reference
| Field | Type | Required | Notes |
|-------|------|----------|-------|
| `name` | `string` | Yes | Unique identifier for this server |
| `command` | `string` | For stdio | Executable (`"docker"`, `"node"`, `"npx"`, etc.) |
| `args` | `string[]` | For stdio | Command arguments |
| `env` | `Record<string, string>` | No | Environment variables merged over process.env |
| `cwd` | `string` | No | Working directory for the subprocess |
| `endpoint` | `string` | For HTTP | URL of the MCP HTTP endpoint |
| `headers` | `Record<string, string>` | No | HTTP auth headers for remote endpoints |
| `transport` | `"stdio"\|"streamable-http"\|"sse"` | No | Auto-inferred if omitted |
## Pitfalls
- `subprocess.kill()` does **not** stop Docker containers — only `docker rm -f` works
- Don't hardcode `transport: "streamable-http"` for HTTP-only Docker servers — let auto-detection handle it
- Container names are PID-scoped — don't try to reference them manually
- Each agent creates its own containers — 10 parallel agents = 10 container instances
- MCP servers connect during `build()`: a server that cannot connect makes `build()` reject with a typed `MCPConnectionError` (`serverName`, `transport`, `status`, `reason: "auth-required"` when it needs a sign-in). Handle it at `build()`, not `run()`
- `env` values must be strings — use `process.env.VAR ?? ""` pattern, never pass `undefined`
- stdio Pattern A requires `-i` in the docker args to keep stdin open; HTTP Pattern B requires `-p PORT:PORT` for host access