MCP服务器模板

一个基于MuleSoft构建的MCP测试服务器,用于展示如何配置和实现可被AI客户端(如Claude或GitHub Copilot)使用的MCP服务器。

  • by Jackx201
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 Jackx201/mcp-server-mulesoft

使用说明

MCP服务器MuleSoft——模板项目

这是一个使用MuleSoft构建的小型MCP(模型上下文协议)测试服务器,用于演示如何配置和实现可供Claude或GitHub Copilot等AI客户端使用的MCP服务器。

什么是MCP?

MCP(模型上下文协议)是一种规范人工智能应用程序如何与外部数据源和工具交互的协议。MCP服务器公开了AI模型可以发现和调用的“工具”。

服务器架构

MCP Client (Agent / Inspector)
    ↓ SSE (Server-Sent Events)
MCP Server (MuleSoft - localhost:8080)
    ↓ Tool Listeners (4 tools exposed)
MuleSoft flows
    ↓ HTTP Requests
JSONPlaceholder API (https://jsonplaceholder.typicode.com)

可用工具

此服务器公开了四个与公共JSONPlaceholder API交互的工具:

1.获取所有帖子

  • 描述:检索所有博客文章
  • 参数:无
  • 回复:100个帖子列表(标题、正文、用户ID)

2.通过身份证获取帖子

  • 描述:通过ID检索单个帖子
  • 参数:
    • postId (整数,1-100):要获取的帖子的ID
  • 回复:单一帖子,详细信息

3.获取用户

  • 描述:检索所有用户的列表
  • 参数:无
  • 回复:用户列表(姓名、电子邮件、城市)

4.获取用户帖子

  • 描述:检索特定用户的所有帖子
  • 参数:
    • userId (整数):用户ID
  • 响应:指定用户的帖子列表

先决条件

  • Java 17
  • Maven 3.x
  • Mule Runtime 4.9.6或更高版本
  • Anypoint Studio(视觉开发可选)

设置和运行

1.打开项目

cd mcp-server-mulesoft

2.启动服务器

选项A:使用Maven

mvn clean install
mvn mule:run

选项B:使用Anypoint Studio

  1. 将项目导入Anypoint Studio
  2. 右键单击项目→ 以...身份运行→ Mule应用程序
  3. 将此VM arg添加到运行配置中: -M-Dmule.http.service.implementation=NETTY

3.验证服务器是否正在运行

curl http://localhost:8080/api/sse

此命令应保持连接打开(按Ctrl+C退出)。

使用MCP检查员进行测试

MCP Inspector是一个用于调试和测试MCP服务器的可视化工具。

npx @modelcontextprotocol/inspector http://localhost:8080/sse

检查器将打开一个浏览器UI,您可以在其中:

  • 查看四种可用工具
  • 使用交互式输入尝试每个工具
  • 实时检查JSON-RPC消息
  • 调试错误和响应

如果正在使用检查器端口:

# Kill the previous process (UNIX example)
kill $(lsof -ti:6274)

# Or use a different port
PORT=6275 npx @modelcontextprotocol/inspector http://localhost:8080/sse

使用cURL进行测试(手动MCP会话)

您还可以使用SSE会话使用cURL手动测试服务器。

# 1. Create an SSE session
curl http://localhost:8080/api/sse

# This call returns a SESSION_ID — use it for subsequent requests

# 2. Initialize the MCP session
curl -X POST "http://localhost:8080/message?sessionId=${SESSION_ID}" \
 -H "Content-Type: application/json" \
 -d '{
    "jsonrpc": "2.0",
    "method": "initialize",
    "params": {
       "protocolVersion": "2024-11-05",
       "capabilities": {},
       "clientInfo": {"name": "test", "version": "1.0.0"}
    },
    "id": 1
 }'

# 2.1 Notify that the MCP session is initialized
curl -X POST "http://localhost:8080/message?sessionId=${SESSION_ID}" \
 -H "Content-Type: application/json" \
 -d '{
    "jsonrpc": "2.0",
    "method": "notifications/initialized",
    "id": 1
 }'

# 3. Call a tool (example: get-users)
curl -X POST "http://localhost:8080/message?sessionId=${SESSION_ID}" \
 -H "Content-Type: application/json" \
 -d '{
    "jsonrpc": "2.0",
    "method": "tools/call",
    "params": {
       "name": "get-users",
       "arguments": {}
    },
    "id": 2
 }'

与您的Copilot代理集成

要从Copilot使用此服务器,请执行以下操作:

  1. 在中添加服务器配置 .vscode/mcp.json:
{
   "servers": {
      "My-MCP-Server": {
         "url": "http://localhost:8080/mcp",
         "type": "http"
      }
   },
   "inputs": []
}
  1. 确保您的聊天机器人在其设置中启用了MCP服务器。
  2. 打开聊天机器人中的工具窗格——您应该看到四个可用的工具。

关键概念

1.MCP服务器配置

<mcp:server-config name="Server" serverName="mule-mcp-server" serverVersion="1.0.0">
    <mcp:streamable-http-server-connection listenerConfig="HTTP-Listener-config"/>
</mcp:server-config>

2.定义工具

<mcp:tool-listener name="tool-name" config-ref="Server">
    <mcp:description>Tool description</mcp:description>
    <mcp:parameters-schema><![CDATA[{
          "$schema": "http://json-schema.org/draft-07/schema#",
          "type": "object",
          "properties": {
                "param1": {
                      "type": "string",
                      "description": "Parameter description"
                }
          },
          "required": ["param1"]
    }]]></mcp:parameters-schema>
    <mcp:responses>
          <mcp:text-tool-response-content text="#[payload.^raw]" />
    </mcp:responses>
</mcp:tool-listener>

3.访问参数

MCP客户端发送的参数可在 payload:

<set-variable variableName="userId" value="#[payload.userId]" />

4.响应格式

确保每个工具都包括 <mcp:responses> 块与 <mcp:text-tool-response-content>:

<mcp:responses>
    <mcp:text-tool-response-content text="#[payload.^raw]" />
</mcp:responses>

5.MCP SSE通信流程

  1. 客户端通过SSE连接: GET /api/sse
  2. 握手: initialize 请求
  3. 发现: tools/list 请求
  4. 执行: tools/call 带参数
  5. 响应:服务器以MCP格式返回结果

项目结构

mcp-server-mulesoft/
├── pom.xml                       # Maven configuration
├── mule-artifact.json            # Mule application metadata
├── src/
│   └── main/
│       ├── mule/
│       │   └── mcp-server-mulesoft.xml   # Flows & tool definitions
│       └── resources/
│           └── log4j2.xml        # Logging configuration
└── README.md                     # This file

故障排除

空响应(“内容:[]”)

原因: <mcp:responses> 配置不正确。

修复:确保每个工具都包括:

<mcp:responses>
    <mcp:text-tool-response-content text="#[payload.^raw]" />
</mcp:responses>

调用外部API时出现SSL/TLS错误

原因:JVM不信任外部API证书。

修复(仅限开发):

<tls:context>
    <tls:trust-store insecure="true"/>
</tls:context>

注意:在生产环境中,将证书添加到Java信任库中。

inputSchema中的“无效输入”

原因:缺失 $schema JSON模式中的字段。

修复:包括 $schema 宣言:

{
   "$schema": "http://json-schema.org/draft-07/schema#",
   "type": "object",
   ...
}

检查器端口6274已在使用中

原因:检查器的另一个实例正在运行。

修复:

  • 在终端中使用Ctrl+C停止正在运行的进程,或
  • 手动终止进程并重新启动检查器:
kill $(lsof -ti:6274)
# Or run Inspector on another port
PORT=6275 npx @modelcontextprotocol/inspector http://localhost:8080/api/sse

其他资源

许可证

这是一个教育开源项目。请随意使用、修改和分发。

演示

一旦您的MCP服务器部署并连接到您的代理。

您可以要求您的代理提供JSON占位符中所有用户的列表。由于它是公共的API,代理可能会尝试执行直接获取;如果发生这种情况,您可以指定它应该使用我们本地MCP服务器中编程的getusers工具。

代理在其响应中提供的数据将完全取决于您提供的提示。您可以通过单击执行工具的对话框来查看完整结果,在那里您将找到包含代理通过使用工具获得的所有数据的原始响应。

同样,代理能够调用需要特定输入参数的工具。

只要它在服务器编程工具的范围内,它就能显著增强代理与通过MCP服务器访问的系统之间的动态信息交换。

将这些原则应用于您自己的MuleSoft MCP服务器,以构建您自己的自定义集成。只需将JSON占位符API换成您需要的任何服务,确保调整您正在实现的特定API的配置