Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.springframework.ai.mcp.client.common.autoconfigure.properties.McpClientCommonProperties;
import org.springframework.ai.mcp.client.common.autoconfigure.properties.McpStdioClientProperties;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand Down Expand Up @@ -73,13 +74,16 @@ public class StdioTransportAutoConfiguration {
* @return list of named MCP transports
*/
@Bean
public List<NamedClientMcpTransport> stdioTransports(McpStdioClientProperties stdioProperties) {
public List<NamedClientMcpTransport> stdioTransports(McpStdioClientProperties stdioProperties,
ObjectProvider<ObjectMapper> objectMapperProvider) {

ObjectMapper objectMapper = objectMapperProvider.getIfAvailable(ObjectMapper::new);

List<NamedClientMcpTransport> stdioTransports = new ArrayList<>();

for (Map.Entry<String, ServerParameters> serverParameters : stdioProperties.toServerParameters().entrySet()) {
var transport = new StdioClientTransport(serverParameters.getValue(),
new JacksonMcpJsonMapper(new ObjectMapper()));
new JacksonMcpJsonMapper(objectMapper));
stdioTransports.add(new NamedClientMcpTransport(serverParameters.getKey(), transport));

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.mcp.client.common.autoconfigure;

import java.util.List;
import java.util.Map;

import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.spec.McpSchema;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Integration tests for {@link StdioTransportAutoConfiguration} with H2.
*
* @author guan xu
*/
@Timeout(15)
@SuppressWarnings("unchecked")
public class StdioTransportAutoConfigurationIT {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(
AutoConfigurations.of(McpClientAutoConfiguration.class, StdioTransportAutoConfiguration.class));

@Test
void connectionsStdioTest() {
this.contextRunner
.withPropertyValues("spring.ai.mcp.client.stdio.connections.server1.command=npx",
"spring.ai.mcp.client.stdio.connections.server1.args[0]=-y",
"spring.ai.mcp.client.stdio.connections.server1.args[1]=@modelcontextprotocol/server-everything")
.run(context -> {
List<McpSyncClient> mcpClients = (List<McpSyncClient>) context.getBean("mcpSyncClients");
assertThat(mcpClients).isNotNull();
assertThat(mcpClients).hasSize(1);

McpSyncClient mcpClient = mcpClients.get(0);
mcpClient.ping();

McpSchema.ListToolsResult toolsResult = mcpClient.listTools();
assertThat(toolsResult).isNotNull();
assertThat(toolsResult.tools()).isNotEmpty();

McpSchema.CallToolResult result = mcpClient.callTool(McpSchema.CallToolRequest.builder()
.name("add")
.arguments(Map.of("operation", "add", "a", 1, "b", 2))
.build());
assertThat(result).isNotNull();
assertThat(result.content()).isNotEmpty();

mcpClient.closeGracefully();
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.ai.mcp.client.common.autoconfigure;

import java.lang.reflect.Field;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.modelcontextprotocol.client.transport.StdioClientTransport;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* Tests for {@link StdioTransportAutoConfiguration}.
*
* @author guan xu
*/
@SuppressWarnings("unchecked")
public class StdioTransportAutoConfigurationTests {

private final ApplicationContextRunner applicationContext = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(StdioTransportAutoConfiguration.class));

@Test
void stdioTransportsNotPresentIfStdioDisabled() {
this.applicationContext.withPropertyValues("spring.ai.mcp.client.enabled", "false")
.run(context -> assertThat(context.containsBean("stdioTransports")).isFalse());
}

@Test
void noTransportsCreatedWithEmptyConnections() {
this.applicationContext.run(context -> {
List<NamedClientMcpTransport> transports = context.getBean("stdioTransports", List.class);
assertThat(transports).isEmpty();
});
}

@Test
void singleConnectionCreateOneTransport() {
this.applicationContext
.withPropertyValues("spring.ai.mcp.client.stdio.connections.server1.command=java",
"spring.ai.mcp.client.stdio.connections.server1.args[0]=--server.port=8080",
"spring.ai.mcp.client.stdio.connections.server1.args[1]=-jar",
"spring.ai.mcp.client.stdio.connections.server1.args[2]=server1.jar",
"spring.ai.mcp.client.stdio.connections.server1.env.API_KEY=sk-abc123")
.run(context -> {
List<NamedClientMcpTransport> transports = context.getBean("stdioTransports", List.class);
assertThat(transports).hasSize(1);
assertThat(transports.get(0).name()).isEqualTo("server1");
assertThat(transports.get(0).transport()).isInstanceOf(StdioClientTransport.class);
});
}

@Test
void multipleConnectionsCreateMultipleTransports() {
this.applicationContext
.withPropertyValues("spring.ai.mcp.client.stdio.connections.server1.command=java",
"spring.ai.mcp.client.stdio.connections.server1.args[0]=--server.port=8080",
"spring.ai.mcp.client.stdio.connections.server1.args[1]=-jar",
"spring.ai.mcp.client.stdio.connections.server1.args[2]=server1.jar",
"spring.ai.mcp.client.stdio.connections.server2.command=python",
"spring.ai.mcp.client.stdio.connections.server2.args[0]=server2.py")
.run(context -> {
List<NamedClientMcpTransport> transports = context.getBean("stdioTransports", List.class);
assertThat(transports).hasSize(2);
assertThat(transports).extracting("name").containsExactlyInAnyOrder("server1", "server2");
assertThat(transports).extracting("transport")
.allMatch(transport -> transport instanceof StdioClientTransport);
});
}

@Test
void serversConfigurationCreateMultipleTransports() {
this.applicationContext
.withPropertyValues("spring.ai.mcp.client.stdio.serversConfiguration=classpath:test-mcp-servers.json")
.run(context -> {
List<NamedClientMcpTransport> transports = context.getBean("stdioTransports", List.class);
assertThat(transports).hasSize(2);
assertThat(transports).extracting("name").containsExactlyInAnyOrder("server1", "server2");
assertThat(transports).extracting("transport")
.allMatch(transport -> transport instanceof StdioClientTransport);
});
}

@Test
void customObjectMapperIsUsed() {
this.applicationContext
.withPropertyValues("spring.ai.mcp.client.stdio.connections.server1.command=java",
"spring.ai.mcp.client.stdio.connections.server1.args[0]=--server.port=8080",
"spring.ai.mcp.client.stdio.connections.server1.args[1]=-jar",
"spring.ai.mcp.client.stdio.connections.server1.args[2]=server1.jar",
"spring.ai.mcp.client.stdio.connections.server1.env.API_KEY=sk-abc123")
.withUserConfiguration(CustomObjectMapperConfiguration.class)
.run(context -> {
assertThat(context.getBean(ObjectMapper.class)).isNotNull();
List<NamedClientMcpTransport> transports = context.getBean("stdioTransports", List.class);
assertThat(transports).hasSize(1);
assertThat(transports.get(0).name()).isEqualTo("server1");
assertThat(transports.get(0).transport()).isInstanceOf(StdioClientTransport.class);
Field privateField = ReflectionUtils.findField(StdioClientTransport.class, "jsonMapper");
ReflectionUtils.makeAccessible(privateField);
assertThat(privateField.get(transports.get(0).transport())).isNotNull();
});
}

@Test
void newObjectMapperIsUsed() {
this.applicationContext
.withPropertyValues("spring.ai.mcp.client.stdio.connections.server1.command=java",
"spring.ai.mcp.client.stdio.connections.server1.args[0]=--server.port=8080",
"spring.ai.mcp.client.stdio.connections.server1.args[1]=-jar",
"spring.ai.mcp.client.stdio.connections.server1.args[2]=server1.jar",
"spring.ai.mcp.client.stdio.connections.server1.env.API_KEY=sk-abc123")
.run(context -> {
assertThatThrownBy(() -> context.getBean(ObjectMapper.class))
.isInstanceOf(NoSuchBeanDefinitionException.class);
List<NamedClientMcpTransport> transports = context.getBean("stdioTransports", List.class);
assertThat(transports).hasSize(1);
assertThat(transports.get(0).name()).isEqualTo("server1");
assertThat(transports.get(0).transport()).isInstanceOf(StdioClientTransport.class);
Field privateField = ReflectionUtils.findField(StdioClientTransport.class, "jsonMapper");
ReflectionUtils.makeAccessible(privateField);
assertThat(privateField.get(transports.get(0).transport())).isNotNull();
});
}

@Configuration
static class CustomObjectMapperConfiguration {

@Bean
ObjectMapper objectMapper() {
return new ObjectMapper();
}

}

}
Loading