Sub topic 16
Connecting external tools through MCP
This chapter assumes you know the agent loop, the built-in tools, and how permissions work. It also assumes you can read JSON and TOML.
What MCP is
The Model Context Protocol (MCP) is an open standard that lets an agent call tools hosted outside its own process. An MCP server exposes named tools, such as "query this database" or "create an issue". The agent is the MCP client. It lists the server's tools, adds them to the set the model can call, and runs them in the same loop as the built-in tools.
Transports
A server runs in one of two ways:
- stdio: the agent starts the server as a local child process and talks to it over standard input and output.
- HTTP: the agent connects to a remote URL. Remote servers often need authentication.
Adding a server
Each tool has a command and a config file for MCP. Claude Code can share a project's servers through a .mcp.json file in the repository root:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "./docs"]
}
}
}Codex CLI reads the same idea from ~/.codex/config.toml:
[mcp_servers.filesystem]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "./docs"]Copilot CLI keeps its servers in its own configuration, and you can manage them from a session with the /mcp slash command. Each tool also has a mcp subcommand, for example claude mcp add and codex mcp add. Check --help for the exact flags in your installed version, because they change between releases.
Permissions still apply
MCP tools go through the same approval system as built-in tools. Expect a prompt the first time the agent calls a new tool. Approve tools one at a time instead of trusting a whole server. Claude Code, for example, lets you allow or deny individual MCP tools by name in its settings.
Common errors
- Server does not start. The command is not on your
PATH, or the Node.js version is too old. Run the command by hand to see the real error. - Tools do not appear. You edited the config during a session. Restart the session or reload from
/mcp. - Secrets committed to the repo. Keep tokens in environment variables, not in a shared
.mcp.json. - Context bloat. Every server adds tool descriptions to the context window. Enable only the servers a task needs.
Exercise
A teammate adds a server to a shared .mcp.json with an API token written in the file. What two problems does this cause, and what is the fix?
Show answer
The token is committed to version control, so everyone with repository access can read it. The server also becomes active for every user who opens the project, which widens the tools the agent can call. The fix is to read the token from an environment variable and have each user approve the server and its tools themselves.