代理编织MCP服务器
Spring AI MCP(模型上下文协议)服务器,具有HTTP同步无状态协议。
先决条件
- Java 17或更高版本
- Maven 3.6+
验证安装:
java -version
mvn -version安装
- 克隆或导航到项目目录:
cd agent-weave-mcp- 无需额外安装-依赖关系由Maven管理。
构建
使用Maven构建项目:
mvn clean package这将:
- 编译源代码
- 运行测试
- 在以下位置创建JAR文件
target/agent-weave-mcp-1.0.0.jar
要在构建过程中跳过测试,请执行以下操作:
mvn clean package -DskipTests启动服务器
选项1:使用JAR文件(推荐)
java -jar target/agent-weave-mcp-1.0.0.jar选项2:使用Maven
mvn spring-boot:run服务器将于启动 http://localhost:9091
您应该看到:
Tomcat started on port 9091 (http) with context path ''
Started AgentWeaveMcpApplication
Registered tools: 3MCP端点
MCP服务器端点位于:
http://localhost:9091/mcp呼叫问候工具
JSON请求格式
向发送POST请求 http://localhost:9091/mcp 具有以下JSON正文:
请求标头:
Accept: application/json, text/event-streamContent-Type: application/json
请求有效载荷:
{
"method": "tools/call",
"params": {
"name": "hello",
"arguments": {},
"_meta": {
"progressToken": 2
}
},
"jsonrpc": "2.0",
"id": 2
}示例:使用cURL
curl -X POST http://localhost:9091/mcp \
-H "Accept: application/json, text/event-stream" \
-H "Content-Type: application/json" \
-d '{
"method": "tools/call",
"params": {
"name": "hello",
"arguments": {},
"_meta": {
"progressToken": 2
}
},
"jsonrpc": "2.0",
"id": 2
}'示例:使用PowerShell
$body = '{"method":"tools/call","params":{"name":"hello","arguments":{},"_meta":{"progressToken":2}},"jsonrpc":"2.0","id":2}'
$headers = @{
"Accept" = "application/json, text/event-stream"
"Content-Type" = "application/json"
}
Invoke-RestMethod -Uri "http://localhost:9091/mcp" `
-Method Post `
-Body $body `
-Headers $headers预期响应
{
"jsonrpc": "2.0",
"id": "1",
"result": {
"content": [
{
"type": "text",
"text": "hello world"
}
]
}
}列出可用工具
要查看所有可用工具,请执行以下操作:
请求标头:
Accept: application/json, text/event-streamContent-Type: application/json
请求有效载荷:
{
"method": "tools/list",
"params": {},
"jsonrpc": "2.0",
"id": 2
}cURL示例
curl -X POST http://localhost:9091/mcp \
-H "Accept: application/json, text/event-stream" \
-H "Content-Type: application/json" \
-d '{
"method": "tools/list",
"params": {},
"jsonrpc": "2.0",
"id": 2
}'配置
服务器使用 无状态 在中配置的协议(HTTP同步无状态) src/main/resources/application.properties:
spring.ai.mcp.server.protocol=STATELESS这提供了:
- 基于HTTP 运输
- 同步 请求响应处理
- 无状态 操作(不维护会话状态)
可用工具
- 你好 -返回“hello world”字符串
- 阅读Outlook电子邮件 -从Outlook邮箱读取电子邮件
- 读数显示 -按ID阅读特定电子邮件
故障排除
端口已在使用中
如果端口9091已在使用中,请将其更改为 src/main/resources/application.properties:
server.port=9092服务器未启动
- 验证Java版本:
java -version(应该是17+) - 检查Maven安装:
mvn -version - 查看服务器日志中的错误
400错误请求错误
- 确保请求正文是有效的JSON
- 验证
Content-Type: application/json标题已设置 - 验证
Accept: application/json, text/event-stream标题已设置 - 检查JSON-RPC格式是否正确
- 确保
_meta.progressToken包含在工具调用的参数中 - 使用MCP检查器进行交互式测试:
npx @modelcontextprotocol/inspector
测试
运行单元测试:
mvn test直接测试hello工具:
mvn test -Dtest=HelloToolTest