MCP入门演示
这是一个全面的演示项目,演示了如何使用Claude Code创建和使用MCP(模型上下文协议)服务器。该项目展示了从服务器实现到与Claude Code集成的完整工作流程。
快速开始
- 克隆并设置MCP服务器
- 导航到应用程序目录并启动Claude Code
- 直接在Claude Code中使用自定义MCP工具
# 1. Setup the server
cd my-library-mcp
uv sync
uv pip install -e .
# 2. Go to app directory and start Claude Code
cd ../app-dir
claude-code
# 3. Test your MCP server in Claude Code
> /mcp list # Should show "my-library" server
> Please process the text "Hello MCP World"项目结构
mcp-with-claude-code-getting-started/
├── README.md # This file - main project documentation
├── my-library-mcp/ # MCP Server implementation
│ ├── my_library/
│ │ └── server.py # Main MCP server code
│ ├── pyproject.toml # Python project configuration
│ └── README.md # Server-specific documentation
└── app-dir/ # Application directory for Claude Code
├── .mcp.json # MCP server configuration for Claude Code
└── .gitignore运作原理
1.MCP服务器(my-library-mcp/)
此目录包含一个功能齐全的MCP服务器,该服务器使用FastMCP构建,提供文本处理功能:
- 服务器名称:
my-library-server - 可用工具:
process_text(text: str)-处理文本并返回原始、大写版本和长度 - 框架:FastMCP(Python)
- 运输: 工作室
2.应用程序目录(app-dir/)
这是运行Claude Code的地方。密钥文件是 .mcp.json 告诉克劳德代码要连接到哪些MCP服务器:
{
"mcpServers": {
"my-library": {
"command": "uv",
"args": [
"run",
"--project", "../my-library-mcp",
"my-library-mcp"
],
"type": "stdio",
"env": {
"PYTHONUNBUFFERED": "1"
}
}
}
}要点:
- 道路
../my-library-mcp与app-dir/ - Claude Code会自动检测并启动中定义的MCP服务器
.mcp.json - 服务器通过运行
uv run进行适当的依赖关系管理
分步设置指南
先决条件
- Python 3.10+
- 紫外线 (Python包管理器)- 安装uv
- 克劳德代码 - 安装Claude代码
步骤1:设置MCP服务器
# Navigate to the server directory
cd my-library-mcp
# Install dependencies
uv sync
# Install the package in development mode (required!)
uv pip install -e .步骤2:验证服务器安装
# Test that the server runs correctly
uv run my-library-mcp您应该看到FastMCP横幅,指示服务器正在运行。
步骤3:开始使用Claude代码
# Navigate to the app directory
cd ../app-dir
# Start Claude Code
claude-code在Claude代码中使用MCP服务器
一旦Claude Code在 app-dir/,您可以:
检查已连接的MCP服务器
> /mcp list您应该看到:
Connected MCP Servers:
- my-library (my-library-server)使用自定义工具
只需让Claude使用您的工具:
> Please process the text "Hello World" using the process_text tool克劳德会这样回应:
I'll process that text for you using the process_text tool.
Result:
- Original text: "Hello World"
- Uppercase: "HELLO WORLD"
- Length: 11 characters了解配置
这 .mcp.json 文件解释
{
"mcpServers": {
"my-library": { // Server name for reference
"command": "uv", // Command to run the server
"args": [ // Arguments for the command
"run",
"--project", "../my-library-mcp", // Path to server project
"my-library-mcp" // Package name to run
],
"type": "stdio", // Communication type
"env": { // Environment variables
"PYTHONUNBUFFERED": "1" // Ensures proper Python output buffering
}
}
}
}Claude代码如何使用此配置
- 自动检测:克劳德代码自动读取
.mcp.json启动时 - 服务器启动:Claude Code将每个配置的服务器作为子进程启动
- 工具注册:Claude可以使用MCP服务器的工具
- 沟通:Claude通过STDIO使用MCP协议与服务器通信
扩展此演示
添加新工具
- 编辑
my-library-mcp/my_library/server.py - 使用添加新功能
@mcp.tool()装饰器 - 重新启动Claude Code以获取新工具
示例:添加新工具
@mcp.tool()
def reverse_text(text: str) -> dict:
"""Reverse the input text and return statistics."""
reversed_text = text[::-1]
return {
"original": text,
"reversed": reversed_text,
"length": len(text)
}添加多个服务器
您可以将多个MCP服务器添加到您的 .mcp.json:
{
"mcpServers": {
"my-library": {
"command": "uv",
"args": ["run", "--project", "../my-library-mcp", "my-library-mcp"],
"type": "stdio"
},
"another-server": {
"command": "node",
"args": ["../another-server/index.js"],
"type": "stdio"
}
}
}故障排除
常见问题
- “没有这样的文件或目录”错误
- 确保你跑了 uv pip install -e . 在服务器目录中 - 验证中的路径 .mcp.json 是正确的
- 服务器未在中显示
/mcp list
- 检查一下 .mcp.json 位于您运行的同一目录中 claude-code - 验证JSON语法是否正确 - 检查服务器日志中的启动错误
- Python环境问题
- 确保你使用的是正确的Python版本(3.10+) - 确保紫外线已安装并正常工作
验证命令
# Check if server package is installed
cd my-library-mcp
uv pip list | grep my-library-mcp
# Test server directly
uv run my-library-mcp
# Check Claude Code MCP connection
cd ../app-dir
claude-code
> /mcp list学习资源
贡献
这是一个演示项目。尝试一下:
- 向MCP服务器添加新工具
- 尝试不同的服务器配置
- 探索高级MCP功能
许可证
MIT许可证-您可以自由地将其用作您自己的MCP项目的基础。
