Skip to content

Commit 40e03ea

Browse files
committed
samples: client, improve comments on configuration files
Signed-off-by: Daniel Garnier-Moiroux <[email protected]>
1 parent ce4ecee commit 40e03ea

File tree

1 file changed

+26
-0
lines changed
  • samples/sample-mcp-client/src/main/java/org/springaicommunity/mcp/security/sample/client

1 file changed

+26
-0
lines changed

samples/sample-mcp-client/src/main/java/org/springaicommunity/mcp/security/sample/client/McpConfiguration.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.springaicommunity.mcp.security.client.sync.oauth2.http.client.OAuth2AuthorizationCodeSyncHttpRequestCustomizer;
2424

2525
import org.springframework.ai.mcp.customizer.McpSyncClientCustomizer;
26+
import org.springframework.ai.model.anthropic.autoconfigure.AnthropicChatAutoConfiguration;
27+
import org.springframework.ai.model.tool.autoconfigure.ToolCallingAutoConfiguration;
2628
import org.springframework.ai.tool.resolution.StaticToolCallbackResolver;
2729
import org.springframework.ai.tool.resolution.ToolCallbackResolver;
2830
import org.springframework.context.annotation.Bean;
@@ -34,6 +36,26 @@
3436
@Configuration
3537
class McpConfiguration {
3638

39+
/**
40+
* If the default {@link ToolCallbackResolver} from
41+
* {@link ToolCallingAutoConfiguration} is imported, then all MCP-based tools are
42+
* added to the resolver. In order to do so, the {@link ToolCallbackResolver} bean
43+
* lists all MCP tools, therefore initializing MCP clients and listing the tools.
44+
* <p>
45+
* This is an issue when the MCP server is secured with OAuth2, because to obtain a
46+
* token, a user must be involved in the flow, and there is no user present on app
47+
* startup.
48+
* <p>
49+
* To avoid this issue, we must exclude the default {@link ToolCallbackResolver}. We
50+
* can't easily disable the entire {@link ToolCallingAutoConfiguration} class, because
51+
* it is imported directly by the chat model configurations, such as
52+
* {@link AnthropicChatAutoConfiguration}. Instead, we provide a default, no-op bean.
53+
*/
54+
@Bean
55+
ToolCallbackResolver resolver() {
56+
return new StaticToolCallbackResolver(List.of());
57+
}
58+
3759
@Bean
3860
McpSyncHttpClientRequestCustomizer requestCustomizer(OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager,
3961
ClientRegistrationRepository clientRegistrationRepository) {
@@ -46,6 +68,10 @@ McpSyncClientCustomizer syncClientCustomizer() {
4668
return (name, syncSpec) -> syncSpec.transportContextProvider(new AuthenticationMcpTransportContextProvider());
4769
}
4870

71+
/**
72+
* Returns the ID of the {@code spring.security.oauth2.client.registration}, if
73+
* unique.
74+
*/
4975
private static String findUniqueClientRegistration(ClientRegistrationRepository clientRegistrationRepository) {
5076
String registrationId;
5177
if (!(clientRegistrationRepository instanceof InMemoryClientRegistrationRepository repo)) {

0 commit comments

Comments
 (0)