Connecting Spring AI MCP Client to HTTP and STDIO tools
Connect Spring AI 2.0.1 MCP Client to Streamable HTTP and STDIO servers, block destructive tools with McpToolFilter allowlists, and wire them into Gemini.
Defining every
@Toolbean inside a single Spring Boot app works fine at first, but it creates tight monolithic coupling as soon as multiple teams own separate capabilities like order management, payments, or internal boards. Model Context Protocol (MCP) decouples LLM applications from tool providers over standard JSON-RPC. This post shows how to connect Spring AI 2.0.1’sspring-ai-starter-mcp-clientto both a remote Streamable HTTP server and a local STDIO CLI server, while blocking destructive tools through anMcpToolFilterallowlist.
Tested with Spring Boot 4.1.1, Spring AI 2.0.1, Kotlin 2.3.21, and Google Gemini.
Why decouple tools with Model Context Protocol
With MCP, the host application doesn’t import domain libraries or depend on the language each tool server is written in—whether Java, Go, Python, or Node.js. Spring AI’s MCP Client handles the JSON-RPC initialize handshake, discovers remote tool schemas, and wraps them as standard ToolCallback instances for ChatClient. (For the protocol fundamentals, see my overview of MCP architecture.)
sequenceDiagram
autonumber
participant Host as Spring Boot (mcp-client)
participant Filter as McpToolFilter (Allowlist)
participant Client as SyncMcpToolCallbackProvider
participant Gemini as Google Gemini
participant OrderServer as order-server (HTTP:8092)
participant ArticleServer as article-server (STDIO)
Host->>OrderServer: POST /mcp (initialize)
Host->>ArticleServer: Child process stdin/stdout (initialize)
OrderServer-->>Host: Tools [get_order_status, cancel_order, ...]
ArticleServer-->>Host: Tools [createArticle, deleteArticle, ...]
Host->>Filter: Pass discovered raw tools
Note over Filter: Strip destructive tools (deleteArticle, cancel_order)
Filter-->>Client: Inject allowlisted tools only
Client->>Gemini: Send ChatClient prompt + allowed schemas
Note over Gemini: Chooses list_orders_by_customer
Gemini-->>Client: FunctionCall("list_orders_by_customer")
Client->>OrderServer: JSON-RPC tools/call (Streamable HTTP)
OrderServer-->>Client: Order list JSON
Client->>Gemini: Return tool output and generate final answer
This layout gives you two immediate architectural benefits:
- Loose coupling: Your AI gateway consumes capabilities over JSON-RPC without sharing database models or deployment cycles with backend microservices.
- Client-side security boundary: Even if an external MCP server exposes destructive mutations like
deleteArticleorcancel_order,McpToolFilterprevents those schemas from ever reaching Gemini.
Project setup and transport configuration
All code in this walkthrough lives in the mcp-client module of spring-ai-examples.
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
plugins {
kotlin("jvm") version "2.3.21"
kotlin("plugin.spring") version "2.3.21"
id("org.springframework.boot") version "4.1.1"
id("io.spring.dependency-management") version "1.1.7"
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.ai:spring-ai-starter-model-google-genai")
implementation("org.springframework.ai:spring-ai-starter-mcp-client")
implementation("tools.jackson.module:jackson-module-kotlin")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
}
Configuring Streamable HTTP and STDIO profiles
Spring AI lets you connect to remote HTTP microservices and local CLI servers side by side via configuration properties.
application-http.yaml connects to a remote mcp-server-webmvc service exposing Streamable HTTP at http://localhost:8092/mcp:
1
2
3
4
5
6
7
8
9
10
11
spring:
ai:
mcp:
client:
type: sync
request-timeout: 30s
streamable-http:
connections:
order-server:
url: http://localhost:8092
endpoint: /mcp
application-stdio.yaml spawns an executable mcp-server-stdio jar as a child process and communicates over stdin/stdout pipes (I cover building both server types in Spring Boot MCP Server with STDIO and WebMVC):
1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
ai:
mcp:
client:
type: sync
request-timeout: 30s
stdio:
connections:
article-server:
command: ${java.home}/bin/java
args:
- "-jar"
- "../mcp-server-stdio/build/libs/mcp-server-stdio-0.1.0.jar"
Choosing between Streamable HTTP and STDIO (and when not to use MCP)
| Transport | Deployment model | Best fit | Main trade-off |
|---|---|---|---|
| Streamable HTTP | Remote microservice | Shared backend tools across pods/teams | Requires network auth, TLS, and timeouts |
| STDIO | Local child process | CLI tools, local dev, desktop agents | Tied to host lifecycle; hard to scale horizontally |
Don’t reach for MCP if you have a single Spring Boot service with 3–5 internal @Tool beans. Wrapping local repository methods in JSON-RPC and HTTP adds serialization overhead, network hops, and extra deployment units for no practical gain.
Blocking destructive tools with McpToolFilter
Never pass every tool advertised by an external MCP server straight to an LLM. A server upgrade might introduce a new write or delete tool that your agent wasn’t authorized to call.
Using a fail-closed allowlist with McpToolFilter ensures only explicitly approved (serverName, toolName) pairs are registered into SyncMcpToolCallbackProvider:
config/McpClientConfig.kt
During startup, each McpSyncClient performs the JSON-RPC initialize handshake and caches the remote server’s Implementation metadata (serverInfo().name()). SyncMcpToolCallbackProvider then calls tools/list on every connected session and passes each discovered tool through any registered McpToolFilter bean before exposing it to Spring AI. By checking serverInfo().name() against our configuration map, a misconfigured or newly upgraded upstream server cannot silently register extra write operations.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Configuration
@EnableConfigurationProperties(McpToolSecurityProperties::class)
class McpClientConfig {
/**
* Per-server tool allowlist filter (fail-closed)
*/
@Bean
fun mcpToolFilter(properties: McpToolSecurityProperties): McpToolFilter {
return McpToolFilter { connectionInfo, tool ->
val serverName = connectionInfo.initializeResult()?.serverInfo()?.name()
// Allow only tools explicitly listed for this server
properties.allowedTools[serverName]?.contains(tool.name()) == true
}
}
/**
* Wire auto-configured MCP tool callbacks into ChatClient as default tools
*/
@Bean
fun chatClient(
builder: ChatClient.Builder,
mcpToolCallbacks: ObjectProvider<SyncMcpToolCallbackProvider>
): ChatClient {
mcpToolCallbacks.ifAvailable { builder.defaultToolCallbacks(it) }
return builder
.defaultSystem("You are an e-commerce and internal board support assistant. Use the connected MCP tools to answer accurately.")
.build()
}
}
application.yaml allowlist properties
1
2
3
4
5
6
7
8
9
10
11
12
13
app:
mcp:
allowed-tools:
article-server:
- createArticle
- getArticle
- listArticles
- updateArticle
# deleteArticle is intentionally omitted and blocked
order-server:
- get_order_status
- list_orders_by_customer
# cancel_order is intentionally omitted and blocked
Figure 1. Comparing raw server tools (/api/mcp/servers) against the filtered allowlist actually exposed to Gemini (/api/mcp/tools).
Verification with STDIO tests and live requests
Run the module test suite, which boots the mcp-server-stdio jar directly to verify the STDIO handshake, allowlist filtering, and Gemini end-to-end tool invocation:
1
./gradlew :mcp-client:test
Figure 2. STDIO child-process handshake, McpToolFilter enforcement, and Gemini MCP end-to-end tests passing.
With order-server running on port 8092, send a natural-language query to /api/mcp/chat:
1
2
3
curl -s -X POST http://localhost:8091/api/mcp/chat \
-H "Content-Type: application/json" \
-d '{"message": "Which of Namju Kim'\''s orders are currently in transit? Include the product name and order ID."}' | jq .
Figure 3. Gemini invoking list_orders_by_customer on the remote order-server over Streamable HTTP and synthesizing the answer.
1
2
3
{
"answer": "Based on Namju Kim's order history, the '27-inch Monitor' (Order ID: ORD-1002) is currently in transit via Hanjin Express."
}
Production caveats and checklist
Avoid running STDIO MCP servers inside horizontally scaled Kubernetes pods unless the child process is strictly stateless and properly reaped on shutdown. Prefer Streamable HTTP for multi-instance production workloads, and keep
McpToolFilterfail-closed so newly added upstream tools aren’t exposed by accident.
Before deploying an MCP Client to production, verify these four items:
- Register a fail-closed
McpToolFilterkeyed byconnectionInfo.initializeResult()?.serverInfo()?.name()so destructive tools (delete*,cancel*) are blocked before reaching the model. - Set an explicit
spring.ai.mcp.client.request-timeout(e.g.,30s) aligned with your upstream microservice SLA so stalled remote tools don’t tie up Tomcat threads. - Use
ObjectProvider<SyncMcpToolCallbackProvider>when wiringChatClientso local unit tests with MCP disabled still boot cleanly. - Pair client-side filtering with server-side authorization—
McpToolFilterstops the model from seeing a tool schema, not a compromised client from calling the endpoint directly.
References
- The Spring AI MCP (Model Context Protocol) Client Boot Starter provides auto-configuration for MCP client functionality in Spring Boot applications. It supports both synchronous and asynchronous client implementations with various transport options.
</div>
</div>