Duck MCP服务器🦆
一个简单的MCP(模型上下文协议)服务器 FastMCP.
特性
此服务器提供以下工具:
- 选择选项:要求用户从提供的选项中选择一个选项(使用启发)
- 提供信息:以自然语言向用户请求更多信息(使用启发)
- 请求手动测试:要求用户进行手动测试并报告结果(使用启发)
安装
先决条件
- Python 3.10或更高版本
- 紫外线 (推荐)或pip
再进行
使用紫外线(推荐):
uv sync使用pip:
pip install -e .用法
运行服务器
使用FastMCP CLI(推荐):
# Run with default configuration from fastmcp.json
fastmcp run
# Or specify the config file explicitly
fastmcp run fastmcp.json
# Run with HTTP transport for testing
fastmcp run --transport http --port 8000直接使用Python:
python server.py使用紫外线运行:
uv run fastmcp run server.py发展模式
使用FastMCP检查器UI运行:
fastmcp dev检查服务器功能
查看所有可用的工具、资源和提示:
fastmcp inspect安装到MCP客户端
克劳德桌面
fastmcp install claude-desktop光标
fastmcp install cursor克劳德代码(VS代码扩展)
fastmcp install claude-code测试
您可以使用FastMCP客户端测试服务器:
import asyncio
from fastmcp import Client
async def test_server():
async with Client("http://localhost:8000/mcp") as client:
# Ping the server to check connectivity
await client.ping()
print("Server is running!")
if __name__ == "__main__":
asyncio.run(test_server())测试启发式工具
这 select_option, provide_information,以及 request_manual_test 工具使用FastMCP的启发功能以交互方式向用户请求信息:
import asyncio
from fastmcp import Client
async def elicitation_handler(message: str, response_type: type, params, context):
"""Handler that responds to server's elicitation requests"""
print(f"Server asks: {message}")
user_input = input("Your response: ")
return response_type(selected_option=user_input) if hasattr(response_type, '__annotations__') and 'selected_option' in response_type.__annotations__ else response_type(information=user_input)
async def test_elicitation():
async with Client("http://localhost:8000/mcp", elicitation_handler=elicitation_handler) as client:
# Test select_option tool
result = await client.call_tool("select_option", {
"question": "What's your favorite programming language?",
"options": ["Python", "JavaScript", "Rust", "Go"]
})
print(result.data)
# Test provide_information tool
result = await client.call_tool("provide_information", {
"question": "What would you like to build today?"
})
print(result.data)
# Test request_manual_test tool
result = await client.call_tool("request_manual_test", {
"test_description": "Navigate to the login page and verify the form renders correctly",
"expected_outcome": "Login form should display username/password fields and submit button"
})
print(result.data)
if __name__ == "__main__":
asyncio.run(test_elicitation())项目结构
duck-mcp/
├── server.py # Main server implementation
├── fastmcp.json # FastMCP configuration
├── pyproject.toml # Project metadata and dependencies
├── README.md # This file
└── tests/ # Test files (optional)发展
添加新工具
要向服务器添加新工具,只需用以下内容装饰功能 @mcp.tool:
@mcp.tool
def my_new_tool(arg1: str, arg2: int) -> str:
"""Description of what this tool does"""
# Your implementation here
return "result"运行测试
pytest部署
本地部署
默认情况下,服务器使用stdio传输运行,使其与Claude Desktop等本地MCP客户端兼容。
HTTP部署
对于远程访问,请使用HTTP传输运行:
fastmcp run --transport http --host 0.0.0.0 --port 8000FastMCP云
部署到FastMCP Cloud进行托管(需要帐户):
fastmcp cloud deploy配置
这 fastmcp.json 文件包含服务器配置:
- 来源:服务器代码的位置和入口点
- 环境:Python版本和依赖关系
- 部署:运行时配置(传输、日志记录等)
您可以通过CLI参数覆盖任何配置:
fastmcp run --port 8080 --log-level DEBUGMCP客户端配置
要将此MCP服务器与MCP兼容的客户端(如Claude Desktop)一起使用,请将以下配置添加到客户端的 mcp.json 文件:
使用紫外线(推荐):
{
"mcpServers": {
"duck-mcp": {
"command": "uv",
"args": ["run", "fastmcp", "run", "server.py"],
"cwd": "/path/to/duck-mcp"
}
}
}直接使用Python:
{
"mcpServers": {
"duck-mcp": {
"command": "python",
"args": ["server.py"],
"cwd": "/path/to/duck-mcp"
}
}
}替换 /path/to/duck-mcp 带有duck mcp目录的实际路径。这 cwd (当前工作目录)确保服务器从正确的位置运行。
了解更多
许可证
麻省理工学院
