mcp

Model Context Protocol (MCP) 是 Anthropic 推出的标准协议,用于 AI 应用与外部工具的集成。

什么是 MCP?

MCP 定义了 AI 应用与工具服务器之间的通信协议:

Claude Code (MCP Client)
  ↕ (MCP Protocol)
MCP Server (提供工具)

协议特性

1

工具发现

// Client → Server
{
  "jsonrpc": "2.0",
  "method": "tools/list",
  "id": 1
}

// Server → Client
{
  "jsonrpc": "2.0",
  "result": {
    "tools": [
      {
        "name": "query_database",
        "description": "Execute SQL query",
        "inputSchema": { ... }
      }
    ]
  },
  "id": 1
}
2

工具调用

// Client → Server
{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "query_database",
    "arguments": {
      "query": "SELECT * FROM users"
    }
  },
  "id": 2
}

// Server → Client
{
  "jsonrpc": "2.0",
  "result": {
    "content": [
      { "type": "text", "text": "..." }
    ]
  },
  "id": 2
}
3

资源管理

// 列出资源
{
  "method": "resources/list"
}

// 读取资源
{
  "method": "resources/read",
  "params": {
    "uri": "file:///path/to/resource"
  }
}
4

提示模板

// 列出提示
{
  "method": "prompts/list"
}

// 获取提示
{
  "method": "prompts/get",
  "params": {
    "name": "code-review"
  }
}

配置 MCP 服务器

配置文件

// .kiro/settings/mcp.json
{
  "mcpServers": {
    "database": {
      "command": "uvx",
      "args": ["mcp-server-postgres"],
      "env": {
        "DATABASE_URL": "postgresql://localhost/mydb"
      },
      "disabled": false,
      "autoApprove": ["query_database"]
    },
    
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed"],
      "disabled": false
    }
  }
}

配置选项

  • command - 启动命令

  • args - 命令参数

  • env - 环境变量

  • disabled - 是否禁用

  • autoApprove - 自动批准的工具列表

MCP Client 实现

连接服务器

工具发现

工具适配器

常用 MCP 服务器

文件系统

数据库

Git

下一步