Skip to content
Merged
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
177 changes: 50 additions & 127 deletions model-context-protocol/mcp-annotations/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,10 @@

<dependencies>

<dependency>
<groupId>org.springaicommunity</groupId>
<artifactId>spring-ai-mcp-annotations</artifactId>
<version>0.2.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-anthropic</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@
import java.util.List;
import java.util.Map;

import org.springaicommunity.mcp.method.elicitation.SyncElicitationSpecification;
import org.springaicommunity.mcp.method.logging.SyncLoggingSpecification;
import org.springaicommunity.mcp.method.progress.SyncProgressSpecification;
import org.springaicommunity.mcp.method.sampling.SyncSamplingSpecification;
import org.springaicommunity.mcp.spring.SyncMcpAnnotationProviders;
import org.springframework.ai.mcp.customizer.McpSyncClientCustomizer;
import org.springframework.ai.mcp.samples.client.customizers.AnnotationSyncClientCustomizer;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -43,49 +35,25 @@ public static void main(String[] args) {
}

@Bean
public CommandLineRunner predefinedQuestions(OpenAiChatModel openAiChatModel,
public CommandLineRunner predefinedQuestions(
List<McpSyncClient> mcpClients) {

return args -> {
McpSyncClient mcpClient = mcpClients.get(0);

// Call a tool that sends progress notifications
CallToolRequest toolRequest = CallToolRequest.builder()
.name("tool1")
.arguments(Map.of("input", "test input"))
.progressToken("test-progress-token")
.build();
for (McpSyncClient mcpClient : mcpClients) {
System.out.println(">>> MCP Client: " + mcpClient.getClientInfo());

CallToolResult response = mcpClient.callTool(toolRequest);
// Call a tool that sends progress notifications
CallToolRequest toolRequest = CallToolRequest.builder()
.name("tool1")
.arguments(Map.of("input", "test input"))
.progressToken("test-progress-token")
.build();

System.out.println("Tool response: " + response);
};
}

@Bean
List<SyncLoggingSpecification> loggingSpecs(McpClientHandlers clientMcpHandlers) {
return SyncMcpAnnotationProviders.loggingSpecifications(List.of(clientMcpHandlers));
}

@Bean
List<SyncSamplingSpecification> samplingSpecs(McpClientHandlers clientMcpHandlers) {
return SyncMcpAnnotationProviders.samplingSpecifications(List.of(clientMcpHandlers));
}
CallToolResult response = mcpClient.callTool(toolRequest);

@Bean
List<SyncElicitationSpecification> elicitationSpecs(McpClientHandlers clientMcpHandlers) {
return SyncMcpAnnotationProviders.elicitationSpecifications(List.of(clientMcpHandlers));
}

@Bean
List<SyncProgressSpecification> progressSpecs(McpClientHandlers clientMcpHandlers) {
return SyncMcpAnnotationProviders.progressSpecifications(List.of(clientMcpHandlers));
}

@Bean
McpSyncClientCustomizer annotationMcpSyncClientCustomizer(List<SyncLoggingSpecification> loggingSpecs,
List<SyncSamplingSpecification> samplingSpecs, List<SyncElicitationSpecification> elicitationSpecs,
List<SyncProgressSpecification> progressSpecs) {
return new AnnotationSyncClientCustomizer(samplingSpecs, loggingSpecs, elicitationSpecs, progressSpecs);
System.out.println("Tool response: " + response);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,36 @@
import io.modelcontextprotocol.spec.McpSchema.ProgressNotification;

@Service
public class McpClientHandlers {
public class McpClientHandlerProviders {

private static final Logger logger = LoggerFactory.getLogger(McpClientHandlers.class);
private static final Logger logger = LoggerFactory.getLogger(McpClientHandlerProviders.class);

@McpProgress(clientId = "server1")
/**
* Handles progress notifications for the client identified by {@code clientId = "server1"}.
* <br>
* The {@code clientId} is configured via application properties, for example:
* <ul>
* <li>{@code spring.ai.mcp.client.sse.connections.server1.url=...}</li>
* <li>{@code spring.ai.mcp.client.streamable-http.connections.server1.url=...}</li>
* </ul>
*
* The handler is assigned only to the client with ID "server1".
*
* @param progressNotification the progress notification received from the server
*/
@McpProgress(clients = "server1")
public void progressHandler(ProgressNotification progressNotification) {
logger.info("MCP PROGRESS: [{}] progress: {} total: {} message: {}",
progressNotification.progressToken(), progressNotification.progress(),
progressNotification.total(), progressNotification.message());
}

@McpLogging
@McpLogging(clients = "server1")
public void loggingHandler(LoggingMessageNotification loggingMessage) {
logger.info("MCP LOGGING: [{}] {}", loggingMessage.level(), loggingMessage.data());
}

@McpSampling
@McpSampling(clients = "server1")
public CreateMessageResult samplingHandler(CreateMessageRequest llmRequest) {
logger.info("MCP SAMPLING: {}", llmRequest);

Expand All @@ -46,7 +59,7 @@ public CreateMessageResult samplingHandler(CreateMessageRequest llmRequest) {
.build();
}

@McpElicitation
@McpElicitation(clients = "server1")
public ElicitResult elicitationHandler(McpSchema.ElicitRequest request) {
logger.info("MCP ELICITATION: {}", request);
return new ElicitResult(ElicitResult.Action.ACCEPT, Map.of("message", request.message()));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
spring.application.name=mcp
spring.main.web-application-type=none

# Disable the chat client auto-configuration because we are using multiple chat models
spring.ai.chat.client.enabled=false

spring.ai.openai.api-key=${OPENAI_API_KEY}
spring.ai.anthropic.api-key=${ANTHROPIC_API_KEY}
# spring.ai.mcp.client.sse.connections.server1.url=http://localhost:8080
spring.ai.mcp.client.streamable-http.connections.server1.url=http://localhost:8080
# spring.ai.mcp.client.streamable-http.connections.server2.url=http://localhost:8081

spring.ai.mcp.client.sse.connections.server1.url=http://localhost:8080
spring.ai.mcp.client.request-timeout=5m

logging.level.io.modelcontextprotocol.client=WARN
logging.level.io.modelcontextprotocol.spec=WARN


spring.ai.mcp.client.toolcallback.enabled=false
# spring.ai.mcp.client.toolcallback.enabled=false
Loading