Consuming MCP Servers From Spring AI: a stdio Child Process and a Remote Streamable HTTP Server, Compared
An assist desk connects to a runbook server it starts as a child process and to the Relay API over HTTP, and its ChatClient gets all four tools. Medians over 50 calls: 0.8 ms on the pipe, 48.9 ms over HTTP. A paused Relay costs 10 s per call and recovers by itself; a killed child stays dead; a missing server stops the host from starting.
What you will learn
- Configure the Spring AI MCP client with one stdio and one Streamable HTTP connection
- Build a stdio MCP server that never writes stray output to stdout
- Send a bearer token to only one connection with an McpClientCustomizer
- Compare the two transports on speed, credentials and failure, with measured numbers
Before you start
- Building an MCP server with Spring AI
- Spring AI tool calling
A host app can use MCP tools from several servers at once. The model sees one list of tools. The transport decides how each call travels. This article connects one Spring Boot host, assist-desk, to two servers: a local runbook server over stdio and the Relay API from the MCP server article over Streamable HTTP. Then it measures and breaks both.
Two connections in YAML
spring:
ai:
# S03L07: one MCP client per server, both started with the app
mcp:
client:
name: assist-desk
type: SYNC
request-timeout: 10s
stdio:
connections:
runbooks: # a local process: the host starts it and owns it
command: java
args:
- -jar
- ../runbooks-server/target/runbooks-server.jar
streamable-http:
connections:
relay: # a remote server: the S03L06 Relay API
url: ${RELAY_URL:http://localhost:8080}
endpoint: /mcp
relay:
token: ${RELAY_TOKEN} # an agent's token, sent on every request to relay
The pom adds spring-ai-starter-mcp-client. A stdio connection is a command: the host starts that jar as its child. A Streamable HTTP connection is a URL. Every tool call may take at most 10 seconds. The chat model is qwen2.5:3b, chosen because it calls tools and llama3.2:3b does not.
A token for one server only
/** S03L07: the remote server wants an agent's token. stdio has no headers: the child process is ours. */
@Configuration
class RelayTokenConfig {
@Bean
McpClientCustomizer<HttpClientStreamableHttpTransport.Builder> relayToken(@Value("${relay.token}") String token) {
return (connection, transport) -> {
if (connection.equals("relay")) {
transport.httpRequestCustomizer((request, method, uri, body, context) ->
request.header("Authorization", "Bearer " + token));
}
};
}
}
The customizer is called once per HTTP connection, with its name. Only relay gets the header, and every POST carries it. The token comes from the environment, never from code.
The stdio server: nothing on stdout but JSON-RPC
# S03L07: a local MCP server. The host starts this jar and talks to it over stdin and stdout.
spring:
main:
web-application-type: none # no port: stdio only
banner-mode: off # stdout carries JSON-RPC and nothing else
ai:
mcp:
server:
name: runbooks
version: 1.0.0
stdio: true
type: SYNC
logging:
pattern:
console: "" # no log line on stdout
file:
name: runbooks-server.log # the log goes to a file instead
Nothing listens on a port. The Spring banner is off, because one stray line on stdout breaks the JSON-RPC stream, and logs go to a file for the same reason. The tools are two read-only @McpTool methods, list_runbooks and read_runbook, serving Markdown files that ship inside the jar.
The host gets every tool
/** S03L07: the host. Its ChatClient gets every tool from both MCP servers. */
AssistController(ChatClient.Builder builder, SyncMcpToolCallbackProvider mcpTools, List<McpSyncClient> clients) {
Map<String, String> serverOf = clients.stream()
.flatMap(c -> c.listTools().tools().stream().map(t -> Map.entry(t.name(), c.getServerInfo().name())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
var tools = Arrays.stream(mcpTools.getToolCallbacks())
.map(t -> new TracedTool(t, serverOf.get(t.getToolDefinition().name())))
.toArray(TracedTool[]::new);
this.chat = builder
.defaultSystem("You help Relay's support agents. Use the tools to look up tickets and runbooks. "
+ "Answer in at most three sentences and name the ticket ids you used.")
.defaultToolCallbacks(tools)
.build();
}
The starter builds one McpSyncClient per connection and a SyncMcpToolCallbackProvider with every tool from both. TracedTool is a small wrapper that implements ToolCallback and logs one line per call: tool, server, arguments and milliseconds. Asking the host what it is connected to:
[{"client":"assist-desk - runbooks","server":"runbooks","version":"1.0.0","tools":["list_runbooks","read_runbook"]},
{"client":"assist-desk - relay","server":"relay","version":"1.0.0","tools":["lookup_kb","search_tickets"]}]
In this run the child process started in 0.806 seconds, inside the host's 2.635. One question that needs both servers:
q=A customer says the export button does nothing. Search the tickets for export, and read the csv-export runbook.
Based on the search results, there is a closed ticket with the subject "Export button does nothing" (ID: 33). The csv-export runbook contains steps for exporting data to CSV, which might be relevant to the customer's issue.
... the host log:
tool: search_tickets on relay {"text":"export"} -> 13 ms
tool: read_runbook on runbooks {"name":"csv-export"} -> 2 ms
Speed
A timing endpoint takes the median of n pings and n calls of one small read-only tool per server, measured as wall-clock time in the host:
$ curl -s "localhost:8088/mcp/timing?n=50"
[{"server":"runbooks","calls":50,"pingMs":0.5,"toolMs":0.8},
{"server":"relay","calls":50,"pingMs":43.7,"toolMs":48.9}]
The Relay call is HTTP, a token check and the database. And this is with Relay in Docker on the same PC. Across a network, add the network.
How each one fails
Remote server paused. Relay was paused with docker compose pause:
[{"server":"runbooks","status":"up"},{"server":"relay","status":"down: ReactiveException: java.util.concurrent.TimeoutException: Did not observe any item or terminal signal within 10000ms ..."}]
q=Search the tickets for export.
It appears we are encountering a timeout issue with the search operation. Let's attempt the search one more time to find the support tickets related to export.
24.323902 s
Ten seconds per call, two calls, a 24-second answer. After docker compose unpause the health check says up again by itself, because each request stands alone.
Remote server restarted. Relay signs with a new key after a restart, and the host read the old token once, at start:
{"server":"relay","status":"down: McpHttpClientTransportAuthorizationException: Authorization error when sending message"}
A new token and a host restart fixed it. In production the token would be refreshed, not read once.
Child process killed. With the runbooks-server process killed, the client waits the full 10 seconds and reports a timeout. The child was started once, when the host started, and nobody starts it again. The model then tried to answer a runbook question with search_tickets instead.
A server missing at start. Each client runs the initialize handshake when the context starts:
... Application run failed
Caused by: java.lang.RuntimeException: Client failed to initialize by explicit API call
Caused by: java.net.ConnectException
Caused by: java.nio.channels.ClosedChannelException
No server, no app. The Java process was still running 30 seconds later, so a supervisor has to stop it.
The two transports side by side
| stdio | Streamable HTTP | |
|---|---|---|
| Deployment | A child of each host, same machine | One server, many hosts |
| Speed here | 0.8 ms a tool call | 48.9 ms a tool call |
| Credentials | None: the host's own user | A token on every request |
| When it dies | Nobody restarts it | Comes back when the server does |
| Missing at startup | Started by the host itself | The host does not start |
The protocol-level view of the same choice is in MCP transports: stdio vs Streamable HTTP.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
What Spring AI 2 Is, and Why a Java Team Wants It
Spring AI 2 is one Spring-style API for AI models: your code talks to a ChatClient, and the provider is a dependency and a setting. Follow one 'I was charged twice' call through advisors and the model to a Java record, see why 20,000 tickets a day is more than one HTTP call, and what the library leaves to you.
Spring AI ChatClient: One API for Any Model, and the Model Is a Property
A small Spring Boot chat service asks llama3.2:3b in Docker through one ChatClient chain, reads 43 tokens in and 60 out from the response, gets the same answer twice at temperature 0 with seed 42, then moves to llama3.2:1b with one environment variable and no code change.
Spring AI Prompt Templates: Placeholders, Few-Shot Messages and Versioned Prompt Files
PromptDesk keeps its prompts in .st files with {customer} and {ticket} placeholders, picks ticket-summary-v1 or v2 with one property, turns a chatty 'I would classify this as BUG' into plain BUG with two example messages, and refuses to send a prompt with a hole in it.
Spring AI Structured Output: From Model JSON to a Java Record, and a Real Ticket Triage Feature
A Triage record with two enums becomes the contract with the model. On llama3.2:3b only native structured output returned a valid record; format text alone left the JSON unclosed and the validation advisor tried three times for the same broken answer. Then Relay's triage: the model picks a category and priority, a config table picks the agent, and a dead model costs the customer one second, not seventy.
