MCP上下文协议

1.3类核心能力
Tools
Resources
Prompts
2.传输方式
stdio
HTTP
SSE
3.从CLI到配置文件
添加本地stdio服务器:启动一个本地文件系统服务,指定工作目录为/workspace
claude mcp add filesystem npx -y @modelcontextprotocol/server-filesystem /workspace

添加远程HTTP服务器:连接至公司内网的Jira服务
claude mcp add --transport http company-jira https://jira.company.com/mcp

添加用户级服务器(全局可用):将GitHub服务添加到用户级别配置,使其对所有项目生效
claude mcp add --scope user github -- npx -y @modelcontextprotocol/server-github

添加带认证信息的服务器:通过自定义HTTP头传递认证Token
claude mcp add --transport http --header "Authorization: Bearer ${TOKEN}" api https://api.example.com/mcp

管理命令
claude mcp list # 列出配置:查看当前所有已配置的 MCP 服务器
claude mcp test github # 测试连接:验证指定服务器(如GitHub)的连接状态及可用性
claude mcp remove github # 移除服务器:删除指定的 MCP 服务器配置

4..mcp.json 配置文件

点击查看代码
{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-filesystem",
        "/workspace"
      ],
      "env": {}
    },
    "company-jira": {
      "type": "http",
      "url": "https://jira.company.com/mcp",
      "headers": {
        "Authorization": "Bearer ${JIRA_TOKEN}"
      }
    },
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres"
      ],
      "env": {
        "DATABASE_URL": "${DB_URL:-postgresql://localhost:5432/mydb}"
      }
    }
  }
}

5.连接数据库

点击查看代码
{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres"
      ],
      "env": {
        "DATABASE_URL": "${DATABASE_URL:-postgresql://localhost:5432/mydb}"
      }
    }
  }
}

6.构建自定义MCP服务器

点击查看代码
import asyncio
import uuid

from mcp.server import Server
from mcp.server.stdio import stdio_server


# 初始化服务器与内存存储
server = Server("todo-server")
todos: list[dict] = []


def generate_id() -> str:
    """生成唯一ID"""
    return str(uuid.uuid4())[:8]


@server.tool("todo_add")
async def add_todo(text: str) -> str:
    """添加新的待办事项"""
    todo_id = generate_id()
    todos.append({"id": todo_id, "text": text, "done": False})
    return f"Added: {todo_id} - {text}"


@server.tool("todo_list")
async def list_todos() -> str:
    """列出所有待办事项"""
    if not todos:
        return "No todos found."
    return "\n".join(
        f"[{'x' if t['done'] else ' '}] {t['id']}: {t['text']}"
        for t in todos
    )


async def main():
    async with stdio_server() as (read_stream, write_stream):
        await server.run(read_stream, write_stream)


if __name__ == "__main__":
    asyncio.run(main())
点击查看代码
{
  "mcpServers": {
    "my-todo": {
      "command": "python",
      "args": [
        "./mcp-server/build/index.js"
      ]
    }
  }
}

7.三层纵深安全机制
第一层,首次连接的交互式审批。
第二层,细粒度的工具级权限控制。
第三层,基于OAuth 2.0的动态认证。

8.安全风险与防护
提示注入攻击
工具权限滥用
冒名顶替

9.MCP+Skills:厨房与菜谱
MCP如同专业厨房
Skills如同标准菜谱

10.常用调试手段
列出所有已配置的服务器及其当前状态
claude mcp list
专门测试特定服务器的连接连通性
claude mcp test my-server
启用MCP调试模式
claude --mcp-debug

11.MCP工具输出的Token管理
自定义配置:用户可以通过设置环境变量MAX_MCP_OUTPUT_TOKENS来调整这一输出上限,以适应不同的业务场景需求

posted @ 2026-08-03 16:41  不知者buwei  阅读(0)  评论(0)    收藏  举报