使用说明
演示MCP服务器
一个最小的FastMCP Python服务器,使用简单的cowsay工具演示基本的MCP(模型上下文协议)功能。
特性
- FastMCP服务器:简单的MCP服务器实现
- 考赛工具:生成奶牛说出自定义消息的ASCII艺术
- 环境配置:使用Pydantic设置支持.env文件
- Docker支持:完整的Docker和Docker组合设置
- 测试:使用pytest进行基本单元测试
- KISS原则:保持简单,集中实施
快速开始
地方发展
-
安装依赖项:
make install-dev -
运行服务器(端口8005上的http模式):
make run-http -
运行测试:
make test
码头工人
-
构建并运行:
make docker-build make docker-run -
对于HTTP传输:
make docker-run-http
项目结构
my-mcp-server/
├── src/
│ ├── config/
│ │ └── settings.py # Environment configuration
│ └── mcp/
│ ├── __main__.py # Entry point
│ ├── server.py # FastMCP server setup
│ └── tools/
│ └── cowsay.py # Cowsay MCP tool
├── tests/ # Unit tests
├── .env.example # Environment variables template
├── docker-compose.yml # Docker compose configuration
├── Dockerfile # Docker container definition
├── Makefile # Common commands
└── pyproject.toml # Python project configuration
可用工具
考赛
生成奶牛说出信息的ASCII艺术。
参数:
message(str):奶牛要说的信息cow(str,可选):要使用的奶牛类型(默认值:“default”)
例子:
result = await cowsay("Hello, World!")
输出:
_______________
< Hello, World! >
---------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
配置
服务器使用环境变量进行配置。复制 .env.example 向 .env 并自定义:
cp .env.example .env
可用设置
| 变量 | 默认值 | 描述 |
|---|---|---|
APP_NAME |
“演示MCP服务器” | 应用程序名称 |
LOG_LEVEL |
“信息” | 记录级别 |
MCP_TRANSPORT |
“stdio” | MCP传输方法 |
MCP_HOST |
“127.0.0.1” | HTTP传输主机 |
MCP_PORT |
“8000” | HTTP传输端口 |
MAX_MESSAGE_LENGTH |
“500” | 最大牛语消息长度 |
与MCP客户端一起使用
克劳德桌面
添加到MCP设置中:
{
"mcpServers": {
"demo-mcp-server": {
"command": "python",
"args": ["-m", "src.mcp"],
"cwd": "/path/to/my-mcp-server"
}
}
}
命令行
# STDIO transport (default)
python -m src.mcp --transport stdio
# HTTP transport
python -m src.mcp --transport http --port 8000
# Help
python -m src.mcp --help
发展
可用的生成命令
make help # Show all available commands
# Development
make install # Install dependencies
make install-dev # Install with dev dependencies
make test # Run tests
make test-verbose # Run tests with verbose output
# Running
make run # Run server locally (STDIO)
make run-http # Run server locally (HTTP)
make health # Check server health
# Docker
make docker-build # Build Docker image
make docker-run # Run in Docker (STDIO)
make docker-run-http # Run in Docker (HTTP)
make docker-stop # Stop containers
make docker-clean # Clean containers and images
# Cleanup
make clean # Clean build artifacts
测试
# Run all tests
make test
# Run with coverage
python -m pytest --cov=src --cov-report=html
# Run specific test
python -m pytest tests/test_cowsay.py -v
添加新工具
- 在中创建新的工具文件
src/mcp/tools/ - 使用FastMCP模式实现该工具
- 在中注册
src/mcp/server.py - 在中添加测试
tests/
工具结构示例:
from fastmcp.tools import Tool
def create_my_tool() -> Tool:
async def my_tool(param: str) -> dict:
# Tool implementation
return {"success": True, "result": param}
return Tool(
name="my_tool",
description="Description of my tool",
func=my_tool,
)
码头工人
建筑
docker-compose build
跑步
STDIO模式(适用于MCP客户端):
docker-compose up demo-mcp-server
HTTP模式(用于测试):
docker-compose --profile http up demo-mcp-http
Docker中的环境变量
创建一个 .env 在中文件或设置环境变量 docker-compose.yml:
environment:
- APP_NAME=My Custom MCP Server
- LOG_LEVEL=DEBUG
健康检查
检查服务器运行状况:
make health
或者直接:
python -c "from src.mcp.server import get_server_health; import json; print(json.dumps(get_server_health(), indent=2))"
许可证
MIT许可证-您可以将其作为您自己的MCP服务器的起点。
贡献
这是一个演示/种子项目。您可以根据自己的需要自由地分叉和扩展它。
MCP开发快乐! 🐄
