Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit 1b6d661

Browse files
committed
code cleaning
1 parent b378ab7 commit 1b6d661

File tree

15 files changed

+191
-100
lines changed

15 files changed

+191
-100
lines changed

mcp/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ The SDK follows a layered architecture:
101101
- **McpClient**: Main interface defining the synchronous operations
102102
- **McpAsyncClient**: Async implementation using Project Reactor
103103
- **McpSyncClient**: Synchronous wrapper around the async client
104-
- **McpAsyncTransport**: Transport layer interface
104+
- **McpTransport**: Transport layer interface
105105
- **DefaultMcpTransport**: Base transport implementation
106106
- **StdioServerTransport**: Stdio-based server communication
107107

mcp/docs/mcp-class-diagram.puml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ skinparam class {
88
}
99

1010
' Interfaces
11-
interface McpAsyncTransport {
11+
interface McpTransport {
1212
+start(): void
1313
+close(): void
1414
+setInboudMessageHandler(Consumer<JSONRPCMessage>): void
@@ -42,16 +42,16 @@ class DefaultMcpTransport {
4242
}
4343

4444
class McpAsyncClient {
45-
-transport: McpAsyncTransport
45+
-transport: McpTransport
4646
-session: McpAsyncSession
47-
+McpAsyncClient(McpAsyncTransport)
47+
+McpAsyncClient(McpTransport)
4848
+initialize(): Mono<InitializeResult>
4949
+close(): void
5050
}
5151

5252
class McpSyncClient {
5353
-asyncClient: McpAsyncClient
54-
+McpSyncClient(McpAsyncTransport)
54+
+McpSyncClient(McpTransport)
5555
+initialize(): InitializeResult
5656
+close(): void
5757
}
@@ -79,22 +79,22 @@ class Assert {
7979
}
8080

8181
' Relationships
82-
McpAsyncTransport <|.. DefaultMcpTransport
82+
McpTransport <|.. DefaultMcpTransport
8383
DefaultMcpTransport <|-- StdioServerTransport
8484

8585
McpClient <|.. McpSyncClient
8686
McpAsyncSession <|.. McpAsyncClient
8787

8888
McpSyncClient o-- McpAsyncClient
89-
McpAsyncClient o-- McpAsyncTransport
89+
McpAsyncClient o-- McpTransport
9090
McpAsyncClient o-- McpAsyncSession
9191

9292
StdioServerTransport o-- ServerParameters
9393
ServerParameters <.. ServerParametersParser : creates
9494

9595
' Package organization
9696
package "spring.ai.mcp.spec" {
97-
' McpAsyncTransport
97+
' McpTransport
9898
' McpAsyncSession
9999
' DefaultMcpTransport
100100
}

mcp/src/main/java/spring/ai/experimental/mcp/client/McpAsyncClient.java

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,36 @@
55
import com.fasterxml.jackson.core.type.TypeReference;
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
import reactor.core.publisher.Mono;
8-
import spring.ai.experimental.mcp.spec.McpAsyncSession;
9-
import spring.ai.experimental.mcp.spec.McpAsyncTransport;
8+
import spring.ai.experimental.mcp.spec.McpSession;
9+
import spring.ai.experimental.mcp.spec.McpTransport;
1010
import spring.ai.experimental.mcp.spec.McpSchema;
1111

12-
public class McpAsyncClient extends McpAsyncSession {
12+
/**
13+
* @author Dariusz Jędrzejczyk
14+
* @author Christian Tzolov
15+
*/
16+
public class McpAsyncClient extends McpSession {
1317

14-
public McpAsyncClient(McpAsyncTransport transport) {
18+
public McpAsyncClient(McpTransport transport) {
1519
this(transport, Duration.ofSeconds(10), new ObjectMapper());
1620
}
1721

18-
public McpAsyncClient(McpAsyncTransport transport, Duration requestTimeout, ObjectMapper objectMapper) {
22+
public McpAsyncClient(McpTransport transport, Duration requestTimeout, ObjectMapper objectMapper) {
1923
super(requestTimeout, objectMapper, transport);
2024
}
2125

22-
2326
/**
24-
* The initialization phase MUST be the first interaction between client and server.
27+
* The initialization phase MUST be the first interaction between client and
28+
* server.
2529
* During this phase, the client and server:
2630
* <ul>
2731
* <li>Establish protocol version compatibility</li>
2832
* <li>Exchange and negotiate capabilities</li>
2933
* <li>Share implementation details</li>
3034
* </ul>
3135
* <br/>
32-
* The client MUST initiate this phase by sending an initialize request containing:
36+
* The client MUST initiate this phase by sending an initialize request
37+
* containing:
3338
* <ul>
3439
* <li>The protocol version the client supports</li>
3540
* <li>The client's capabilities</li>
@@ -38,14 +43,16 @@ public McpAsyncClient(McpAsyncTransport transport, Duration requestTimeout, Obje
3843
*
3944
* The server MUST respond with its own capabilities and information:
4045
* {@link McpSchema.ServerCapabilities}. <br/>
41-
* After successful initialization, the client MUST send an initialized notification
46+
* After successful initialization, the client MUST send an initialized
47+
* notification
4248
* to indicate it is ready to begin normal operations.
4349
*
4450
* <br/>
4551
*
4652
* <a href=
4753
* "https://github.com/modelcontextprotocol/specification/blob/main/docs/specification/basic/lifecycle.md#initialization">Initialization
4854
* Spec</a>
55+
*
4956
* @return the initialize result.
5057
*/
5158
public Mono<McpSchema.InitializeResult> initialize() {
@@ -54,8 +61,9 @@ public Mono<McpSchema.InitializeResult> initialize() {
5461
new McpSchema.ClientCapabilities(null, new McpSchema.ClientCapabilities.RootCapabilities(true), null),
5562
new McpSchema.Implementation("mcp-java-client", "0.0.1")); // @formatter:on
5663

57-
Mono<McpSchema.InitializeResult> result =
58-
this.sendRequest("initialize", initializeRequest, new TypeReference<McpSchema.InitializeResult>() {});
64+
Mono<McpSchema.InitializeResult> result = this.sendRequest("initialize", initializeRequest,
65+
new TypeReference<McpSchema.InitializeResult>() {
66+
});
5967

6068
return result.flatMap(initializeResult -> {
6169
if (!McpSchema.LATEST_PROTOCOL_VERSION.equals(initializeResult.protocolVersion())) {
@@ -64,7 +72,7 @@ public Mono<McpSchema.InitializeResult> initialize() {
6472
+ initializeResult.protocolVersion()));
6573
} else {
6674
return this.sendNotification("notifications/initialized", null)
67-
.thenReturn(initializeResult);
75+
.thenReturn(initializeResult);
6876
}
6977
});
7078
}
@@ -81,29 +89,34 @@ public Mono<Void> sendRootsListChanged() {
8189
*/
8290
public Mono<Void> ping() {
8391
return this.sendRequest("ping", null,
84-
new TypeReference<Void>() {});
92+
new TypeReference<Void>() {
93+
});
8594
}
8695

8796
// --------------------------
8897
// Tools
8998
// --------------------------
9099
/**
91100
* Send a tools/call request.
101+
*
92102
* @param callToolRequest the call tool request.
93103
* @return the call tool result.
94104
*/
95105
public Mono<McpSchema.CallToolResult> callTool(McpSchema.CallToolRequest callToolRequest) {
96106
return this.sendRequest("tools/call", callToolRequest,
97-
new TypeReference<McpSchema.CallToolResult>() {});
107+
new TypeReference<McpSchema.CallToolResult>() {
108+
});
98109
}
99110

100111
/**
101112
* Send a tools/list request.
113+
*
102114
* @return the list of tools result.
103115
*/
104116
public Mono<McpSchema.ListToolsResult> listTools(String cursor) {
105117
return this.sendRequest("tools/list", new McpSchema.PaginatedRequest(cursor),
106-
new TypeReference<McpSchema.ListToolsResult>() {});
118+
new TypeReference<McpSchema.ListToolsResult>() {
119+
});
107120
}
108121

109122
// --------------------------
@@ -112,16 +125,19 @@ public Mono<McpSchema.ListToolsResult> listTools(String cursor) {
112125

113126
/**
114127
* Send a resources/list request.
128+
*
115129
* @param cursor the cursor
116130
* @return the list of resources result.
117131
*/
118132
public Mono<McpSchema.ListResourcesResult> listResources(String cursor) {
119133
return this.sendRequest("resources/list", new McpSchema.PaginatedRequest(cursor),
120-
new TypeReference<McpSchema.ListResourcesResult>() {});
134+
new TypeReference<McpSchema.ListResourcesResult>() {
135+
});
121136
}
122137

123138
/**
124139
* Send a resources/read request.
140+
*
125141
* @param resource the resource to read
126142
* @return the resource content.
127143
*/
@@ -131,56 +147,69 @@ public Mono<McpSchema.ReadResourceResult> readResource(McpSchema.Resource resour
131147

132148
/**
133149
* Send a resources/read request.
150+
*
134151
* @param readResourceRequest the read resource request.
135152
* @return the resource content.
136153
*/
137154
public Mono<McpSchema.ReadResourceResult> readResource(McpSchema.ReadResourceRequest readResourceRequest) {
138155
return this.sendRequest("resources/read", readResourceRequest,
139-
new TypeReference<McpSchema.ReadResourceResult>() {});
156+
new TypeReference<McpSchema.ReadResourceResult>() {
157+
});
140158
}
141159

142160
/**
143161
* Resource templates allow servers to expose parameterized resources using URI
144162
* templates. Arguments may be auto-completed through the completion API.
145163
*
146164
* Request a list of resource templates the server has.
165+
*
147166
* @param cursor the cursor
148167
* @return the list of resource templates result.
149168
*/
150169
public Mono<McpSchema.ListResourceTemplatesResult> listResourceTemplates(String cursor) {
151170
return this.sendRequest("resources/templates/list", new McpSchema.PaginatedRequest(cursor),
152-
new TypeReference<McpSchema.ListResourceTemplatesResult>() {});
171+
new TypeReference<McpSchema.ListResourceTemplatesResult>() {
172+
});
153173
}
154174

155175
/**
156-
* List Changed Notification. When the list of available resources changes, servers
176+
* List Changed Notification. When the list of available resources changes,
177+
* servers
157178
* that declared the listChanged capability SHOULD send a notification:
158179
*/
159180
public Mono<Void> sendResourcesListChanged() {
160181
return this.sendNotification("notifications/resources/list_changed");
161182
}
162183

163184
/**
164-
* Subscriptions. The protocol supports optional subscriptions to resource changes.
165-
* Clients can subscribe to specific resources and receive notifications when they
185+
* Subscriptions. The protocol supports optional subscriptions to resource
186+
* changes.
187+
* Clients can subscribe to specific resources and receive notifications when
188+
* they
166189
* change.
167190
*
168191
* Send a resources/subscribe request.
169-
* @param subscribeRequest the subscribe request contains the uri of the resource to
170-
* subscribe to.
192+
*
193+
* @param subscribeRequest the subscribe request contains the uri of the
194+
* resource to
195+
* subscribe to.
171196
*/
172197
public Mono<Void> subscribeResource(McpSchema.SubscribeRequest subscribeRequest) {
173198
return this.sendRequest("resources/subscribe", subscribeRequest,
174-
new TypeReference<Void>() {});
199+
new TypeReference<Void>() {
200+
});
175201
}
176202

177203
/**
178204
* Send a resources/unsubscribe request.
179-
* @param unsubscribeRequest the unsubscribe request contains the uri of the resource
180-
* to unsubscribe from.
205+
*
206+
* @param unsubscribeRequest the unsubscribe request contains the uri of the
207+
* resource
208+
* to unsubscribe from.
181209
*/
182210
public Mono<Void> unsubscribeResource(McpSchema.UnsubscribeRequest unsubscribeRequest) {
183211
return this.sendRequest("resources/unsubscribe", unsubscribeRequest,
184-
new TypeReference<Void>() {});
212+
new TypeReference<Void>() {
213+
});
185214
}
186215
}

mcp/src/main/java/spring/ai/experimental/mcp/client/McpClient.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
import java.time.Duration;
1919

2020
import com.fasterxml.jackson.databind.ObjectMapper;
21-
import spring.ai.experimental.mcp.spec.McpAsyncTransport;
21+
import spring.ai.experimental.mcp.spec.McpTransport;
2222

2323
/**
2424
* The MCP client is the main entry point for interacting with the Model Context Protocol
2525
* (MCP) server.
2626
*
27-
*
2827
* @author Christian Tzolov
29-
* @since 1.0.0
28+
* @author Dariusz Jędrzejczyk
3029
*/
3130
public class McpClient {
3231

@@ -40,20 +39,20 @@ private McpClient() {
4039
// JSONtoPOJOCodec type
4140
// .sync();
4241

43-
public static McpAsyncClient async(McpAsyncTransport transport) {
42+
public static McpAsyncClient async(McpTransport transport) {
4443
return new McpAsyncClient(transport);
4544
}
4645

47-
public static McpAsyncClient async(McpAsyncTransport transport, Duration requestTimeout,
46+
public static McpAsyncClient async(McpTransport transport, Duration requestTimeout,
4847
ObjectMapper objectMapper) {
4948
return new McpAsyncClient(transport, requestTimeout, objectMapper);
5049
}
5150

52-
public static McpSyncClient sync(McpAsyncTransport transport) {
51+
public static McpSyncClient sync(McpTransport transport) {
5352
return new McpSyncClient(async(transport));
5453
}
5554

56-
public static McpSyncClient sync(McpAsyncTransport transport, Duration requestTimeout,
55+
public static McpSyncClient sync(McpTransport transport, Duration requestTimeout,
5756
ObjectMapper objectMapper) {
5857
return new McpSyncClient(async(transport, requestTimeout, objectMapper));
5958
}

mcp/src/main/java/spring/ai/experimental/mcp/client/McpSyncClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import spring.ai.experimental.mcp.client.util.Assert;
66
import spring.ai.experimental.mcp.spec.McpSchema;
77

8+
/**
9+
* @author Dariusz Jędrzejczyk
10+
* @author Christian Tzolov
11+
*/
812
public class McpSyncClient implements AutoCloseable {
913

1014
// TODO: Consider providing a client config to set this properly

mcp/src/main/java/spring/ai/experimental/mcp/client/stdio/ServerParameters.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
/**
1515
* Server parameters for stdio client.
16+
*
17+
* @author Christian Tzolov
18+
* @author Dariusz Jędrzejczyk
1619
*/
1720
@JsonInclude(JsonInclude.Include.NON_ABSENT)
1821
public class ServerParameters {

mcp/src/main/java/spring/ai/experimental/mcp/client/stdio/ServerParametersParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
/**
2727
* @author Christian Tzolov
28-
* @since 1.0.0
28+
* @author Dariusz Jędrzejczyk
2929
*/
3030

3131
public class ServerParametersParser {

mcp/src/main/java/spring/ai/experimental/mcp/client/stdio/StdioServerTransport.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
/**
2323
* Stdio client for communicating with a server process.
24+
*
25+
* @author Christian Tzolov
26+
* @author Dariusz Jędrzejczyk
2427
*/
2528
public class StdioServerTransport extends DefaultMcpTransport {
2629

mcp/src/main/java/spring/ai/experimental/mcp/spec/DefaultMcpTransport.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
/**
2727
* @author Christian Tzolov
28-
* @since 1.0.0
28+
* @author Dariusz Jędrzejczyk
2929
*/
30-
public class DefaultMcpTransport implements McpAsyncTransport {
30+
public class DefaultMcpTransport implements McpTransport {
3131

3232
protected final ObjectMapper objectMapper;
3333

@@ -93,7 +93,7 @@ public void setInboudMessageHandler(Consumer<JSONRPCMessage> inboundMessageHandl
9393
this.inboundMessageHandler = inboundMessageHandler;
9494
}
9595

96-
public void setErrorHandler(Consumer<String> errorHandler) {
96+
public void setInboundErrorHandler(Consumer<String> errorHandler) {
9797
this.errorHandler = errorHandler;
9898
}
9999

0 commit comments

Comments
 (0)