
ModelContextProtocolServer

2025.03.01
0
Java工具管理开发工具集成开发效率
ModelContextProtocolServer(MCP Server)是一个支持多种传输模式(如SSE和stdio)的服务,提供工具调用和管理的功能。其核心功能包括工具列表查询、工具调用(如计算器和天气查询)等,适用于需要集成多种工具功能的开发场景。
View on GitHub
Overview
基本能力
产品定位
ModelContextProtocolServer(MCP Server)是一个支持工具调用和管理的服务,适用于开发效率提升的场景。
核心功能
- 工具列表查询:通过
tools/list
方法获取所有可用工具的列表及其描述。 - 工具调用:支持通过
tools/call
方法调用具体工具(如计算器和天气查询)。 - 多种传输模式:支持SSE(Server-Sent Events)和stdio(标准输入输出)两种传输模式。
适用场景
- 开发环境中需要集成多种工具功能的场景。
- 需要动态查询和调用工具的服务。
工具列表
- calculator:基础计算器,支持加、减、乘、除等操作。
- get_current_weather:获取指定位置的当前天气信息。
常见问题解答
- 如何连接SSE流?
bash curl -N -H "Accept: text/event-stream" http://localhost:8081/sse
- 如何启动stdio服务器?
bash java -Dtransport.mode=stdio \ -Dspring.main.web-application-type=none \ -Dspring.main.banner-mode=off \ -Dlogging.file.name=mcpserver.log \ -jar target/mcpserver-0.0.1-SNAPSHOT.jar
使用教程
使用依赖
- 确保已安装Java和Maven。
- 生成Java artifact:
bash ./mvnw clean install
安装教程
- 启动stdio服务器:
bash java -Dtransport.mode=stdio \ -Dspring.main.web-application-type=none \ -Dspring.main.banner-mode=off \ -Dlogging.file.name=mcpserver.log \ -jar target/mcpserver-0.0.1-SNAPSHOT.jar
调试方式
- 查询工具列表:
bash curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' http://localhost:8081/mcp/message
- 调用计算器工具:
bash curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"calculator","arguments":{"operation":"+","a":2,"b":3}}}' http://localhost:8081/mcp/message
- 调用天气查询工具:
bash curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"get_current_weather","arguments":{"location":"San Francisco","format":"celsius"}}}' http://localhost:8081/mcp/
许可证
None