Skip to content

Conversation

Fj-ivy
Copy link

@Fj-ivy Fj-ivy commented Oct 16, 2025

Summary

I believe there might be two issues with the implementation of Function<Initialization, Mono> postInitializationHook in the constructor of the McpAsyncClient class.

  1. invalid check condition about enableCallToolSchemaCaching condition
  2. filter tool.outputSchema() using tool.outputSchema() != null

Optimization Details

The following optimizations have been made regarding the above two issues;

  1. Remove the invalid check condition if (enableCallToolSchemaCaching && listToolsResult.tools() != null)
  2. Using !Utils.isEmpty(tool.outputSchema()) instead of tool.outputSchema() !=null to filter.
  3. Merge the log printing into the entire Lambda expression.

Comparison

Before

Function<Initialization, Mono<Void>> postInitializationHook = init -> {

			if (init.initializeResult().capabilities().tools() == null || !enableCallToolSchemaCaching) {
				return Mono.empty();
			}

			return this.listToolsInternal(init, McpSchema.FIRST_PAGE).doOnNext(listToolsResult -> {
				listToolsResult.tools()
					.forEach(tool -> logger.debug("Tool {} schema: {}", tool.name(), tool.outputSchema()));
				if (enableCallToolSchemaCaching && listToolsResult.tools() != null) {
					// Cache tools output schema
					listToolsResult.tools()
						.stream()
						.filter(tool -> tool.outputSchema() != null)
						.forEach(tool -> this.toolsOutputSchemaCache.put(tool.name(), tool.outputSchema()));
				}
			}).then();
		};

After:

Function<Initialization, Mono<Void>> postInitializationHook = init -> {

			if (init.initializeResult().capabilities().tools() == null || !enableCallToolSchemaCaching) {
				return Mono.empty();
			}

			return this.listToolsInternal(init, McpSchema.FIRST_PAGE)
                    .doOnNext(listToolsResult ->
                        listToolsResult.tools()
                                .stream()
                                .filter(tool -> !Utils.isEmpty(tool.outputSchema()))
                                .forEach(tool -> {
                                    logger.debug("Tool {} schema: {}", tool.name(), tool.outputSchema());
                                    // Put tool output schema to cache
                                    this.toolsOutputSchemaCache.put(tool.name(), tool.outputSchema());
                                })
			    ).then();
		};

@Fj-ivy Fj-ivy changed the title fix: invalid check condition and use Utils.isEmpty instead of tool.ou… fix: optimize the postInitializationHook in McpAsyncClient Oct 16, 2025
…tputSchema()!=null

Signed-off-by: fangjie <fangjiewd@126.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant