Skip to content

Commit de3d331

Browse files
committed
chore: Make SyncSpec and AsyncSpec extensible
Signed-off-by: He-Pin <hepin1989@gmail.com>
1 parent bc30857 commit de3d331

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

mcp-core/src/main/java/io/modelcontextprotocol/client/McpClient.java

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public interface McpClient {
121121
* @throws IllegalArgumentException if transport is null
122122
*/
123123
static SyncSpec sync(McpClientTransport transport) {
124-
return new SyncSpec(transport);
124+
return new McpClient.SyncSpec(transport);
125125
}
126126

127127
/**
@@ -156,44 +156,46 @@ static AsyncSpec async(McpClientTransport transport) {
156156
* <li>Change notification handlers for tools, resources, and prompts
157157
* <li>Custom message sampling logic
158158
* </ul>
159+
*
160+
* This class can be extended by subclass.
159161
*/
160162
class SyncSpec {
161163

162-
private final McpClientTransport transport;
164+
protected final McpClientTransport transport;
163165

164-
private Duration requestTimeout = Duration.ofSeconds(20); // Default timeout
166+
protected Duration requestTimeout = Duration.ofSeconds(20); // Default timeout
165167

166-
private Duration initializationTimeout = Duration.ofSeconds(20);
168+
protected Duration initializationTimeout = Duration.ofSeconds(20);
167169

168-
private ClientCapabilities capabilities;
170+
protected ClientCapabilities capabilities;
169171

170-
private Implementation clientInfo = new Implementation("Java SDK MCP Client", "0.15.0");
172+
protected Implementation clientInfo = new Implementation("Java SDK MCP Client", "0.15.0");
171173

172-
private final Map<String, Root> roots = new HashMap<>();
174+
protected final Map<String, Root> roots = new HashMap<>();
173175

174-
private final List<Consumer<List<McpSchema.Tool>>> toolsChangeConsumers = new ArrayList<>();
176+
protected final List<Consumer<List<McpSchema.Tool>>> toolsChangeConsumers = new ArrayList<>();
175177

176-
private final List<Consumer<List<McpSchema.Resource>>> resourcesChangeConsumers = new ArrayList<>();
178+
protected final List<Consumer<List<McpSchema.Resource>>> resourcesChangeConsumers = new ArrayList<>();
177179

178-
private final List<Consumer<List<McpSchema.ResourceContents>>> resourcesUpdateConsumers = new ArrayList<>();
180+
protected final List<Consumer<List<McpSchema.ResourceContents>>> resourcesUpdateConsumers = new ArrayList<>();
179181

180-
private final List<Consumer<List<McpSchema.Prompt>>> promptsChangeConsumers = new ArrayList<>();
182+
protected final List<Consumer<List<McpSchema.Prompt>>> promptsChangeConsumers = new ArrayList<>();
181183

182-
private final List<Consumer<McpSchema.LoggingMessageNotification>> loggingConsumers = new ArrayList<>();
184+
protected final List<Consumer<McpSchema.LoggingMessageNotification>> loggingConsumers = new ArrayList<>();
183185

184-
private final List<Consumer<McpSchema.ProgressNotification>> progressConsumers = new ArrayList<>();
186+
protected final List<Consumer<McpSchema.ProgressNotification>> progressConsumers = new ArrayList<>();
185187

186-
private Function<CreateMessageRequest, CreateMessageResult> samplingHandler;
188+
protected Function<CreateMessageRequest, CreateMessageResult> samplingHandler;
187189

188-
private Function<ElicitRequest, ElicitResult> elicitationHandler;
190+
protected Function<ElicitRequest, ElicitResult> elicitationHandler;
189191

190-
private Supplier<McpTransportContext> contextProvider = () -> McpTransportContext.EMPTY;
192+
protected Supplier<McpTransportContext> contextProvider = () -> McpTransportContext.EMPTY;
191193

192-
private JsonSchemaValidator jsonSchemaValidator;
194+
protected JsonSchemaValidator jsonSchemaValidator;
193195

194-
private boolean enableCallToolSchemaCaching = false; // Default to false
196+
protected boolean enableCallToolSchemaCaching = false; // Default to false
195197

196-
private SyncSpec(McpClientTransport transport) {
198+
public SyncSpec(McpClientTransport transport) {
197199
Assert.notNull(transport, "Transport must not be null");
198200
this.transport = transport;
199201
}
@@ -496,42 +498,44 @@ public McpSyncClient build() {
496498
* <li>Change notification handlers for tools, resources, and prompts
497499
* <li>Custom message sampling logic
498500
* </ul>
501+
*
502+
* This class can be extended by subclass.
499503
*/
500504
class AsyncSpec {
501505

502-
private final McpClientTransport transport;
506+
protected final McpClientTransport transport;
503507

504-
private Duration requestTimeout = Duration.ofSeconds(20); // Default timeout
508+
protected Duration requestTimeout = Duration.ofSeconds(20); // Default timeout
505509

506-
private Duration initializationTimeout = Duration.ofSeconds(20);
510+
protected Duration initializationTimeout = Duration.ofSeconds(20);
507511

508-
private ClientCapabilities capabilities;
512+
protected ClientCapabilities capabilities;
509513

510-
private Implementation clientInfo = new Implementation("Java SDK MCP Client", "0.15.0");
514+
protected Implementation clientInfo = new Implementation("Java SDK MCP Client", "0.15.0");
511515

512-
private final Map<String, Root> roots = new HashMap<>();
516+
protected final Map<String, Root> roots = new HashMap<>();
513517

514-
private final List<Function<List<McpSchema.Tool>, Mono<Void>>> toolsChangeConsumers = new ArrayList<>();
518+
protected final List<Function<List<McpSchema.Tool>, Mono<Void>>> toolsChangeConsumers = new ArrayList<>();
515519

516-
private final List<Function<List<McpSchema.Resource>, Mono<Void>>> resourcesChangeConsumers = new ArrayList<>();
520+
protected final List<Function<List<McpSchema.Resource>, Mono<Void>>> resourcesChangeConsumers = new ArrayList<>();
517521

518-
private final List<Function<List<McpSchema.ResourceContents>, Mono<Void>>> resourcesUpdateConsumers = new ArrayList<>();
522+
protected final List<Function<List<McpSchema.ResourceContents>, Mono<Void>>> resourcesUpdateConsumers = new ArrayList<>();
519523

520-
private final List<Function<List<McpSchema.Prompt>, Mono<Void>>> promptsChangeConsumers = new ArrayList<>();
524+
protected final List<Function<List<McpSchema.Prompt>, Mono<Void>>> promptsChangeConsumers = new ArrayList<>();
521525

522-
private final List<Function<McpSchema.LoggingMessageNotification, Mono<Void>>> loggingConsumers = new ArrayList<>();
526+
protected final List<Function<McpSchema.LoggingMessageNotification, Mono<Void>>> loggingConsumers = new ArrayList<>();
523527

524-
private final List<Function<McpSchema.ProgressNotification, Mono<Void>>> progressConsumers = new ArrayList<>();
528+
protected final List<Function<McpSchema.ProgressNotification, Mono<Void>>> progressConsumers = new ArrayList<>();
525529

526-
private Function<CreateMessageRequest, Mono<CreateMessageResult>> samplingHandler;
530+
protected Function<CreateMessageRequest, Mono<CreateMessageResult>> samplingHandler;
527531

528-
private Function<ElicitRequest, Mono<ElicitResult>> elicitationHandler;
532+
protected Function<ElicitRequest, Mono<ElicitResult>> elicitationHandler;
529533

530-
private JsonSchemaValidator jsonSchemaValidator;
534+
protected JsonSchemaValidator jsonSchemaValidator;
531535

532-
private boolean enableCallToolSchemaCaching = false; // Default to false
536+
protected boolean enableCallToolSchemaCaching = false; // Default to false
533537

534-
private AsyncSpec(McpClientTransport transport) {
538+
public AsyncSpec(McpClientTransport transport) {
535539
Assert.notNull(transport, "Transport must not be null");
536540
this.transport = transport;
537541
}

0 commit comments

Comments
 (0)