从 MCP SDK 的发展史上看,FastMCP 是前辈,SolonMCP 则是后辈。mcp-python-sdk 功能完善,已经很成熟了。而 mcp-java-sdk 却还不完善,比如:
- 还不支持 http streaming
- 还不支持 resouce template,不过有 pr 在走流程了(SolonMCP 提前提供了支持)
- 只支持 jdk17+(SolonMCP 提供了 jdk8+ 支持)
- 不支持 客户端断线自动重连(SolonMCP 提供了自动重连支持)
两者的体验不能说是一样,但真的很像。
1、FastMCP 的开发体验(python)
计算器工具
python
@mcp.tool()
def add(a: int, b: int) -> int:
"""将两个数字相加"""
return a + b
@mcp.tool()
def subtract(a: int, b: int) -> int:
"""从第一个数中减去第二个数"""
return a - b
@mcp.tool()
def multiply(a: int, b: int) -> int:
"""将两个数相乘"""
return a * b
@mcp.tool()
def divide(a: float, b: float) -> float:
"""将第一个数除以第二个数"""
if b == 0:
raise ValueError("除数不能为零")
return a / b
if __name__ == "__main__":
# 使用stdio传输方式启动服务器
mcp.run(transport="stdio")
天气工具(有工具,资源,资源模板)
python
@mcp.tool()
def get_weather(city: str) -> dict:
"""获取指定城市的当前天气"""
return "24度,晴"
@mcp.resource("weather://cities")
def get_available_cities() -> list:
"""获取所有可用的城市列表"""
return ["Tokyo", "Sydney", "Tokyo"]
@mcp.resource("weather://forecast/{city}")
def get_forecast(city: str) -> dict:
"""获取指定城市的天气预报资源"""
return {
"city": city,
"temperature": [10,25],
"condition":['sunny', 'clear', 'hot'],
"unit": "celsius"
}
if __name__ == "__main__":
# 使用SSE传输方式启动服务器
mcp.run(transport="sse")
2、SolonMCP 的开发体验(java)
SolonMCP(全称:solon-ai-mcp),支持 java8,可提供完成的 mcp 内容支持(工具,资源,资源模板,提示语)。
计算器工具
java
@McpServerEndpoint(channel = McpChannel.STDIO)
public class CalculatorTools {
@ToolMapping(description = "将两个数字相加")
public int add(@Param int a, @Param int b) {
return a + b;
}
@ToolMapping(description = "从第一个数中减去第二个数")
public int subtract(@Param int a, @Param int b) {
return a - b;
}
@ToolMapping(description = "将两个数相乘")
public int multiply(@Param int a, @Param int b) {
return a * b;
}
@ToolMapping(description = "将第一个数除以第二个数")
public float divide(@Param float a, @Param float b) {
return a / b;
}
}
天气工具(有工具,资源,资源模板)
java
@McpServerEndpoint(sseEndpoint = "/mcp/sse")
public class WeatherTools {
@ToolMapping(description = "获取指定城市的当前天气")
public String get_weather(@Param(description="城市") String city) {
return "{city: '" + city + "', temperature:[10,25], condition:['sunny', 'clear', 'hot'], unit:celsius}";
}
//可以给前端用,输出严格的 json 格式
@Produces(MimeType.APPLICATION_JSON_VALUE)
@ResourceMapping(uri = "weather://cities", description = "获取所有可用的城市列表")
public List<String> get_available_cities() {
return Arrays.asList("Tokyo", "Sydney", "Tokyo");
}
@ResourceMapping(uri = "weather://forecast/{city}", description = "获取指定城市的天气预报资源")
public String get_forecast(@Param(description="城市") String city) {
return "{city: '" + city + "', temperature:[10,25], condition:['sunny', 'clear', 'hot'], unit:celsius}";
}
}