Windows 标准I/O配置
在Windows上,像npx、npm和node这样的命令是作为批处理文件(.cmd)实现的,而不是本机可执行文件。Java的ProcessBuilder无法直接执行批处理文件,需要使用cmd.exe /c包装器。
为什么Windows需要特殊处理
当Java的ProcessBuilder(由StdioClientTransport内部使用)尝试在Windows上启动一个进程时,它只能执行以下操作:
原生可执行文件(.exe文件)
可用于cmd.exe的系统命令
Windows 批处理文件,如 npx.cmd、npm.cmd,甚至来自 Microsoft Store 的 python.cmd,都需要使用 cmd.exe shell 来执行。
解决方案:cmd.exe 包装器
使用 cmd.exe /c 包裹批处理文件命令:
Windows配置:
{ "mcpServers": { "filesystem": { "command": "cmd.exe", "args": [ "/c", "npx", "-y", "@modelcontextprotocol/server-filesystem", "C:\\Users\\username\\Desktop" ] } }}
Linux/macOS配置:
{ "mcpServers": { "filesystem": { "command": "npx", "args": [ "-y", "@modelcontextprotocol/server-filesystem", "/Users/username/Desktop" ] } }}
跨平台编程配置
对于需要在不同平台上运行而无需单独配置文件的应用程序,请在您的Spring Boot应用程序中使用操作系统检测:
@Bean(destroyMethod = "close")@ConditionalOnMissingBean(McpSyncClient.class)public McpSyncClient mcpClient() { ServerParameters stdioParams; if (isWindows()) { // Windows: cmd.exe /c npx approach var winArgs = new ArrayList<>(Arrays.asList( "/c", "npx", "-y", "@modelcontextprotocol/server-filesystem", "target")); stdioParams = ServerParameters.builder("cmd.exe") .args(winArgs) .build(); } else { // Linux/Mac: direct npx approach stdioParams = ServerParameters.builder("npx") .args("-y", "@modelcontextprotocol/server-filesystem", "target") .build(); } return McpClient.sync(new StdioClientTransport(stdioParams, McpJsonMapper.createDefault())) .requestTimeout(Duration.ofSeconds(10)) .build() .initialize();}privatestatic boolean isWindows() { return System.getProperty("os.name").toLowerCase().contains("win");}
在使用@Bean进行编程式配置时,请添加@ConditionalOnMissingBean(McpSyncClient.class)以避免与JSON文件中的自动配置发生冲突。
路径考虑
相对路径(推荐用于可移植性):
{ "command": "cmd.exe", "args": ["/c", "npx", "-y", "@modelcontextprotocol/server-filesystem", "target"]}
MCP服务器根据应用程序的工作目录解析相对路径。
绝对路径(Windows系统需要使用反斜杠或转义的正斜杠):
{ "command": "cmd.exe", "args": ["/c", "npx", "-y", "@modelcontextprotocol/server-filesystem", "C:\\Users\\username\\project\\target"]}
需要使用cmd.exe的常见Windows批处理文件
参考实现
请参阅Spring AI示例 - 文件系统,以获取一个完整的跨平台MCP客户端实现,该实现能自动检测操作系统并相应地配置客户端。
可流式HTTP传输属性
用于连接到Streamable-HTTP和无状态Streamable-HTTP MCP服务器。
流式HTTP传输的属性以spring.ai.mcp.client.streamable-http为前缀:
| 描述 | 默认值 |
|---|
connections
| 命名Streamable-HTTP连接配置地图 | - |
connections.[name].url
| 用于与MCP服务器进行Streamable-Http通信的基本URL端点 | - |
connections.[name].endpoint
| 用于连接的streamable-http端点(作为url后缀) | /mcp
|
示例配置:
spring: ai: mcp: client: streamable-http: connections: server1: url: http://localhost:8080 server2: url: http://otherserver:8081 endpoint: /custom-sse
SSE传输属性
服务器发送事件(SSE)传输的属性以spring.ai.mcp.client.sse为前缀:
| 描述 | 默认值 |
|---|
connections
| 命名SSE连接配置地图 | - |
connections.[name].url
| 用于与MCP服务器进行SSE通信的基本URL端点 | - |
connections.[name].sse-endpoint
| 用于连接的sse端点(作为url后缀) | /sse
|
示例配置:
spring: ai: mcp: client: sse: connections: # Simple configuration using default /sse endpoint server1: url: http://localhost:8080 # Custom SSE endpoint server2: url: http://otherserver:8081 sse-endpoint: /custom-sse # Complex URL with path and token (like MCP Hub) mcp-hub: url: http://localhost:3000 sse-endpoint: /mcp-hub/sse/cf9ec4527e3c4a2cbb149a85ea45ab01 # SSE endpoint with query parameters api-server: url: https://api.example.com sse-endpoint: /v1/mcp/events?token=abc123&format=json
URL拆分指南
当你拥有一个完整的SSE(服务器发送事件)URL时,请将其拆分为基础URL和端点路径:
| 配置 |
|---|
http://localhost:3000/mcp-hub/sse/token123
| url: localhost:3000sse-endpoint: /mcp-hub/sse/token123
|
https://api.service.com/v2/events?key=secret
| url: api.service.comsse-endpoint: /v2/events?key=secret
|
http://localhost:8080/sse
| url: localhost:8080sse-endpoint: /sse(或默认省略)
|
SSE连接故障排除
404 Not Found错误:
验证URL拆分:确保基础URL仅包含方案、主机和端口
检查sse-endpoint是否以“/”开头,并且包含完整的路径和查询参数
直接在浏览器或使用curl命令测试完整URL,以确认其是否可访问
可流式传输的HTTP传输属性
可流式处理的Http传输的属性以spring.ai.mcp.client.streamable-http为前缀:
| 描述 | 默认值 |
|---|
connections
| 命名的Streamable Http连接配置地图 | - |
connections.[name].url
| 用于与MCP服务器进行Streamable-Http通信的基本URL端点 | - |
connections.[name].endpoint
| 用于连接的streamable-http端点(作为url后缀) | /mcp
|
示例配置:
spring: ai: mcp: client: streamable-http: connections: server1: url: http://localhost:8080 server2: url: http://otherserver:8081 endpoint: /custom-sse