Sub topic 16
Connecting external tools through MCP
This chapter assumes you have read the chapters on the agent loop, built-in tools, and permissions. You should be comfortable editing JSON and TOML files and running npx commands.
What MCP is and why it exists
The Model Context Protocol (MCP) is an open protocol that lets an agent call tools that live outside the agent. The built-in tools cover file edits, shell commands, and search. MCP adds tools for things like an issue tracker, a database, a browser, or an internal API.
Claude Code, Codex CLI, and Copilot CLI all support MCP. A server written once works with all three.
The protocol has two roles:
- Client: the coding agent. It connects to servers, lists their tools, and passes the tool definitions to the model.
- Server: a program that exposes tools (and sometimes resources and prompts). It runs a tool when the client asks and returns the result.
From the model's side, an MCP tool is another entry in the tool list. The agent loop does not change. The model emits a tool call, the agent runs it, and the result goes back as an observation.
Two transport types
A server is reached in one of two ways.
| Transport | How it runs | Typical use |
|---|---|---|
| stdio (local) | The agent starts the server as a child process and talks over standard input and output. | Local tools, filesystem access, scripts |
| HTTP (remote) | The agent connects to a URL, often with OAuth or a token header. | Hosted services such as issue trackers |
A stdio server runs with your user's permissions. It is a program on your machine, not a sandboxed plugin. A remote server runs elsewhere, but it still receives whatever the model sends it.
Adding a server in each tool
All three tools can add servers from the command line or from a config file. The following example uses the reference filesystem server, restricted to a docs directory.
Claude Code
The command line writes the config for you:
claude mcp add filesystem -- npx -y @modelcontextprotocol/server-filesystem ./docs
claude mcp listClaude Code stores servers at one of three scopes: local (only you, this project), project (shared through a file in the repository), and user (all your projects). The project scope uses a .mcp.json file at the repository root. You can commit it so the whole team gets the same servers:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "./docs"]
}
}
}Claude Code asks you to approve a project-scoped server before it uses it for the first time. Inside a session, /mcp shows server status and handles sign-in for remote servers.
Codex CLI
Codex CLI reads servers from the [mcp_servers.<name>] tables in ~/.codex/config.toml. The codex mcp add command writes the same entries.
[mcp_servers.filesystem]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "./docs"]Copilot CLI
Copilot CLI keeps its servers in ~/.copilot/mcp-config.json. You can also add them interactively with the /mcp add slash command.
{
"mcpServers": {
"filesystem": {
"type": "local",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "./docs"],
"tools": ["*"]
}
}
}The tools field selects which of the server's tools are exposed. ["*"] exposes all of them. Listing specific names is a cheap way to cut the attack surface.
Command names, file locations, and field names change between releases. Check each tool's current documentation if a command here fails.
How MCP tools meet permissions
MCP tools go through the same approval system as built-in tools. By default the agent asks before it calls a tool from a server you have not trusted.
In Claude Code, MCP tools are named mcp__<server>__<tool>. You can use that name in the permission rules you saw earlier:
{
"permissions": {
"allow": ["mcp__filesystem__read_file"],
"deny": ["mcp__filesystem__write_file"]
}
}A rule such as mcp__filesystem with no tool name matches every tool from that server. Prefer allowing named read-only tools over allowing a whole server.
Sandboxing does not protect you here in the way you might expect. The sandbox limits commands the agent runs through its shell tool. A stdio MCP server is a separate process, and its own behavior and reach decide what it can touch. Treat each server as code you are choosing to run.
Costs: context and noise
Each connected server adds its tool names, descriptions, and schemas to the model's context. Large servers can expose dozens of tools. That uses context-window space on every turn and can make the model pick the wrong tool.
- Connect only the servers a task needs.
- Disable or filter tools you never use.
- Remove a server from the session when you finish with it.
- Watch for tool results that return huge payloads, because those also fill the context.
Failure modes and debugging
| Symptom | Likely cause | What to check |
|---|---|---|
| Server shows as failed or disconnected | The command is not on `PATH`, or `npx` cannot download the package | Run the exact command in your shell and read the error |
| Tools do not appear | The server started but listed no tools, or a tool filter excludes them | Check `/mcp` and the filter fields |
| Remote server returns an authorization error | The OAuth sign-in or token expired | Sign in again from the session |
| Works in a terminal, fails in the agent | Different environment variables | Set variables in the server's `env` entry, not only in your shell profile |
| Agent ignores a tool | The description is vague, or too many tools are loaded | Reduce the tool list and mention the tool by name in your prompt |
Security risks specific to MCP
MCP widens what a prompt injection can reach. Prompt injection is text in data the model reads that tries to give the model new instructions. If a server returns an issue body, a web page, or a database row, that text enters the model's context. An attacker who controls the text can try to steer the agent into using your other tools.
The dangerous combination is access to private data, exposure to untrusted content, and a way to send data out. A tracker server that reads public issues, plus a filesystem server that reads your .env, plus a shell that can reach the network, covers all three.
Reduce the risk with these steps:
- Install servers only from sources you trust, and pin versions rather than using
@latest. - Give servers the narrowest credentials. Use a read-only token where writes are not needed.
- Keep approvals on for tools that write, send, or delete.
- Never put secrets in a committed
.mcp.json. Reference environment variables instead. - Read a project-scoped server's command before you approve it. A cloned repository can ship a config that starts an arbitrary program.
Exercise
You add an MCP server for your issue tracker to a project, and the agent starts choosing the wrong tool for file edits. Give two reasons this can happen and two changes that reduce it.
Show answer
Reasons: the server's many tool definitions use up context and compete with the built-in tools, and vague tool descriptions make the model pick poorly. Changes: restrict the server to the few tools you need (for example with a tool filter or an allow list), and remove or disable the server for tasks that do not involve the tracker. Naming the intended tool in your prompt also helps.
Exercise
A teammate commits a .mcp.json that runs npx some-package@latest. Name one risk and one mitigation.
Show answer
Risk: @latest can change to a malicious or broken version, and the command runs with your user's permissions once approved. Mitigation: pin an exact version, review the package, and approve the server only if you trust it.