MCP(model context protocol)
2025年7月2日大约 1 分钟
MCP Service 市场
https://smithery.ai/
https://mcpmarket.com/
https://mcp.so/
https://docs.astral.sh/uv/guides/tools/
魔塔mcp广场
playwright
Server
The FastMCP server is your core interface to the MCP protocol. It handles connection management, protocol compliance, and message routing:
Resources
Resources are how you expose data to LLMs. They're similar to GET endpoints in a REST API - they provide data but shouldn't perform significant computation or have side effects:
mcp 模式
stdio(默认): 通过标准的输入输出来进行数据的交互
sse: 直接通过http 接口来访问服务的
pip install mcp
from mcp.server.fastmcp import FastMCP
from mcp.types import TextContent, Tool
# 创建 FastMCP 实例
mcp = FastMCP("Hello World Appender")
# 定义工具函数
@mcp.tool()
def append_hello_world(text: str) -> str:
"""
将用户输入的文本后面拼接 'hello world'
Args:
text: 用户输入的文本内容
Returns:
拼接后的字符串
"""
return f"{text} hello world"
# 如果需要作为独立服务运行
if __name__ == "__main__":
import asyncio
# 运行服务器
asyncio.run(mcp.run())
import random
from typing import Optional
from mcp.server.fastmcp import FastMCP
mcp = FastMCP(
name="Demo 🚀",
port=8090
)
@mcp.tool()
def get_poker_cards(num: int = 5) -> str:
# 定义花色和牌面
suits = ['♠', '♥', '♦', '♣']
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
# 创建一副牌
deck = [f"{suit}{rank}"for suit in suits for rank in ranks]
# 随机抽取n张牌
random_cards = random.sample(deck, num)
return",".join(random_cards)
if __name__ == "__main__":
# 初始化并运行服务器
mcp.run(transport="sse")