Chapter 7: MCP Servers
What is MCP?
MCP (Model Context Protocol) is a standard protocol developed by Anthropic to connect AI models with external data sources and services.
Core Concepts:
- 📡 Unified Interface: Standardized data access
- 🔌 Extensible: Easily add new data sources
- 🔒 Secure: Fine-grained permission control
- ⚡ Efficient: Real-time data access
What can MCP do?
- Connect databases (PostgreSQL, MySQL, MongoDB, etc.)
- Access cloud services (AWS, Google Cloud, Azure)
- Integrate dev tools (GitHub, Jira, Slack)
- Read local filesystem
- Call external APIs
Configuring MCP Servers
Configuration File Location
7.2 配置 MCP 服务器
~/.config/claude-code/mcp-servers.jsonBasic Configuration Structure
json
{
"mcpServers": {
"server-name": {
"command": "node",
"args": ["/path/to/server.js"],
"env": {
"API_KEY": "your-key"
}
}
}
}Example: Filesystem Server
7.3 常用 MCP 服务器
json
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/home/user/projects"
]
}
}
}Popular MCP Servers
1. Filesystem Server
Install:
bash
npm install -g @modelcontextprotocol/server-filesystemConfigure:
json
{
"mcpServers": {
"filesystem": {
"command": "mcp-server-filesystem",
"args": ["/home/user/documents"]
}
}
}Usage:
bash
You: read content of /documents/report.pdf
Claude: [Using MCP filesystem server]
Reading report.pdf...
[Content summary]2. GitHub Server
Configure:
json
{
"mcpServers": {
"github": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-github"
],
"env": {
"GITHUB_TOKEN": "ghp_xxxxx"
}
}
}
}Usage:
bash
You: get my GitHub repository list
Claude: [Using MCP GitHub server]
Found 25 repositories:
1. user/project-a (⭐ 123)
2. user/project-b (⭐ 45)
...3. PostgreSQL Server
Configure:
json
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres"
],
"env": {
"DATABASE_URL": "postgresql://user:pass@localhost/db"
}
}
}
}Usage:
7.4 自定义 MCP 服务器
bash
You: query total user count in database
Claude: [Using MCP Postgres server]
SELECT COUNT(*) FROM users;
Result: 1,234 usersCustom MCP Servers
Create Simple MCP Server
javascript
// my-mcp-server.js
const { Server } = require('@modelcontextprotocol/sdk/server/index.js');
const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');
const server = new Server({
name: 'my-custom-server',
version: '1.0.0',
}, {
capabilities: {
tools: {},
},
});
// Define tools
server.setRequestHandler('tools/list', async () => {
return {
tools: [{
name: 'get_weather',
description: 'Get weather for a city',
inputSchema: {
type: 'object',
properties: {
city: {
type: 'string',
description: 'City name',
},
},
required: ['city'],
},
}],
};
});
// Handle tool calls
server.setRequestHandler('tools/call', async (request) => {
if (request.params.name === 'get_weather') {
const { city } = request.params.arguments;
// Implement weather query logic
return {
content: [{
type: 'text',
text: `Weather in ${city}: Sunny, 25°C`,
}],
};
}
});
// Start server
const transport = new StdioServerTransport();
await server.connect(transport);Configure Custom Server
7.5 MCP 实战案例
json
{
"mcpServers": {
"weather": {
"command": "node",
"args": ["/path/to/my-mcp-server.js"]
}
}
}MCP Practical Examples
Case 1: Database Query Assistant
bash
You: query top 10 products by revenue
Claude: [Using MCP Postgres]
SELECT name, SUM(revenue) as total
FROM products p
JOIN sales s ON p.id = s.product_id
GROUP BY p.id, p.name
ORDER BY total DESC
LIMIT 10;
Top 10 Products:
1. Product A - $125,000
2. Product B - $98,500
...Case 2: GitHub Automation
bash
You: create a new issue to report login bug
Claude: [Using MCP GitHub]
Creating issue in user/repo...
✓ Issue created: #156
Title: "Bug: Login malfunction"
Labels: bug, priority:high
Assignee: userCase 3: Documentation Generation
bash
You: generate API docs from database
Claude: [Using MCP Postgres + filesystem]
1. Querying database schema...
2. Analyzing table relationships...
3. Generating documentation...
✓ Created api-docs.mdSummary
In this chapter, we learned:
- ✅ MCP protocol concept and purpose
- ✅ How to configure MCP servers
- ✅ Popular MCP servers (filesystem, GitHub, database)
- ✅ Create custom MCP servers
- ✅ MCP practical examples
Key Takeaways:
- MCP extends Claude Code's capabilities
- Supports various data sources and services
- Can develop custom MCP servers
- Security configuration is important
Next Step: Chapter 8 introduces the subagent system!