Skip to content

Chapter 10 · MCP Integration and External Tools ​

Chapter goals: Master Claude Code's MCP (Model Context Protocol) integration, learn to configure MCP servers with stdio/SSE transport, and使用 common tool extensions to expand Claude's capability boundaries.

10.1 The Role of MCP in Claude Code ​

MCP lets Claude Code突破 built-in tools (file read/write, Bash) and connect to any external service:

text
┌─────────────────┐     MCP Protocol      ┌──────────────────┐
│   Claude Code    │ ◄──────────────► │   MCP Server      │
│                  │                   │                  │
│ Built-in tools:   │    stdio/SSE     │  filesystem      │
│   Read/Write     │    JSON-RPC      │  github          │
│   Bash/Grep      │ ◄──────────────► │  postgres        │
│                  │                   │  slack           │
│ MCP tools:       │                   │  puppeteer       │
│   mcp__fs__read  │                   │  ...custom        │
└─────────────────┘                   └──────────────────┘

Each MCP server exposes a组 of tools (tools), and Claude automatically selects which to call based on the task. Tool names follow the format mcp__<server>__<tool>.

10.2 Configuring MCP Servers ​

Command-line Addition ​

bash
# Basic syntax
claude mcp add <name> <command> [args...]

# Add a filesystem server — let Claude safely access specified directories
claude mcp add filesystem -- npx -y @modelcontextprotocol/server-filesystem /path/to/dir

# Add a GitHub server
claude mcp add github -- npx -y @modelcontextprotocol/server-github

# Add an SSE remote server
claude mcp add my-api --transport sse https://my-server.com/mcp

# List configured servers
claude mcp list

# Remove
claude mcp remove filesystem

Config File Method ​

json
// .claude/settings.json or ~/.claude/settings.json
{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/home/user/projects"]
    },
    "github": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_TOKEN": "ghp_xxx"
      }
    }
  }
}

Scope ​

ScopeFlagEffective Range
ProjectdefaultCurrent directory only
User--scope userAll projects
Global--scope globalCross-project sharing

10.3 Transport Protocol Deep Dive ​

stdio (Standard Input/Output) ​

The most commonly used local transport:

text
Claude Code ──stdin──►  MCP Server process
            ◄─stdout──
  • Server spawns as a subprocess;
  • Exchanges JSON-RPC messages via stdin/stdout;
  • Suitable for本地 scripts and npm-package-style servers.

SSE (Server-Sent Events) ​

For remote HTTP services:

bash
claude mcp add remote-tools --transport sse https://mcp.example.com/sse \
  --header "Authorization: Bearer token123"
  • Claude Code acts as an SSE client connecting to a remote endpoint;
  • Suitable for team-shared centralized MCP services.

10.4 Common MCP Server Walkthrough ​

filesystem — Filesystem Access ​

bash
claude mcp add fs -- npx -y @modelcontextprotocol/server-filesystem ~/docs ~/notes

After adding, Claude can:

  • mcp__fs__read_file — read files in the specified directory
  • mcp__fs__write_file — create or modify files
  • mcp__fs__list_directory — list directory contents
  • mcp__fs__search_files — search files

github — GitHub API Operations ​

bash
export GITHUB_TOKEN="ghp_your_token"
claude mcp add gh -- npx -y @modelcontextprotocol/server-github
text
> Help me create an issue in the anthropics/claude-code repo,
  titled "MCP integration guide", description: "need to supplement MCP config docs"

Claude will调用 the mcp__gh__create_issue tool to complete the operation.

postgres — Database Queries ​

bash
claude mcp add db -- npx -y @modelcontextprotocol/server-postgres \
  "postgresql://user:pass@localhost:5432/mydb"
text
> Query the number of users registered in the last 7 days grouped by day

Claude executes SQL via mcp__db__query and returns results.

10.5 Tool Discovery and Permission Control ​

Viewing Available Tools ​

bash
# List all currently available MCP tools
> /mcp

# Output:
# ┌────────────────────────────────────────────┐
# │ MCP Servers                                │
# ├────────────────────────────────────────────┤
# │ ✅ filesystem (4 tools)                    │
# │ ✅ github (12 tools)                       │
# │ ❌ postgres — connection refused           │
# └────────────────────────────────────────────┘

Permission Control ​

MCP tools require user confirmation by default; pre-approve in settings.json:

json
{
  "permissions": {
    "allow": [
      "mcp__filesystem__read_file",
      "mcp__filesystem__list_directory",
      "mcp__github__get_issue"
    ],
    "deny": [
      "mcp__github__delete_repository"
    ]
  }
}

Least Privilege Principle

Only allow tools you trust. Especially delete_*, write_*类 tools — a malicious MCP server could cause data loss.

Chapter Summary ​

  • MCP configured via claude mcp add; supports stdio (local) and SSE (remote) transport;
  • Common servers: filesystem (files), github (code hosting), postgres (database);
  • Tool naming format mcp__<server>__<tool>; view status with /mcp;
  • Fine-grained MCP tool permissions via allow/deny in settings.json;
  • Follow least privilege — only open必要 tools.

🧪 随堂测验

点击你认为正确的选项。答错时会展示正确答案与原因解析。

1. What is the naming format for MCP tools in Claude Code?

2. What is the main difference between stdio and SSE transport?

3. How do you让 Claude auto-execute a specific MCP tool without asking every time?

4. Which MCP server lets Claude directly query a PostgreSQL database?

🛠️ Hands-on Practice ​

  1. Use claude mcp add to add a filesystem server and have it read a text file on your desktop.
  2. Configure a GitHub MCP server (needs a personal access token), then have Claude list open issues for a repo.
  3. Try configuring a non-existent MCP server; observe the error提示 from /mcp, then fix it.

After completing the exercises, proceed to Chapter 11: Skills System.