🚀 模型上下文協議服務器
本項目的模型上下文協議服務器提供了一系列 API 接口,可用於連接 SSE 流、stdio,以及發送工具列表請求和調用不同工具等操作,方便開發者進行相關功能的開發和測試。
🚀 快速開始
🔍 連接到 SSE 流
curl -N -H "Accept: text/event-stream" http://localhost:8081/sse
🔌 連接到 stdio
生成 Java 資產
./mvnw clean install
啟動 stdio 服務器
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
💻 使用示例
基礎用法
📄 發送 tool/list 請求
curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' http://localhost:8081/mcp/message
event:message
data:{"jsonrpc":"2.0","id":2,"result":{"tools":[{"name":"calculator","description":"Basic calculator","inputSchema":{"type":"object","properties":{"operation":{"type":"string"},"a":{"type":"number"},"b":{"type":"number"}},"required":["operation","a","b"]}},{"name":"get_current_weather","description":"Get the current weather in a given location","inputSchema":{"type":"object","properties":{"location":{"type":"string","description":"The name of the city e.g. San Francisco, CA"},"format":{"type":"string","enum":["celsius","fahrenheit"],"description":"The format to return the weather in"}},"required":["location"]}}]}}
高級用法
🧮 調用計算器工具
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
event:message
data:{"jsonrpc":"2.0","id":1,"result":{"content":[{"type":"text","type":"text","text":"2.00 + 3.00 = 5.00"}],"isError":false}}
🌤️ 調用天氣工具
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/
event:message
data:{"jsonrpc":"2.0","id":1,"result":{"content":[{"type":"text","type":"text","text":"Current weather in San Francisco, United States of America: Partly cloudy, 11.1°C, Precipitation: 0.0 mm"}],"isError":false}}