FastMCP(python)和 SolonMCP(java)的体验比较(不能说一样,但真的很像)

从 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}";
    }
}
相关推荐
Derek_Smart28 分钟前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
曲幽1 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
NE_STOP1 小时前
MyBatis-mybatis入门与增删改查
java
孟陬5 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌5 小时前
一站式了解四种限流算法
java·后端·go
华仔啊5 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java
敏编程6 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪6 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
也些宝6 小时前
Java单例模式:饿汉、懒汉、DCL三种实现及最佳实践
java