Skip to content

TransportContext for Client side #479

@olamy

Description

@olamy

Thanks for adding TransportContext on server-side, a similar feature is missing for client side as well.
My plan is to use a single McpClientTransport assuming it's thread safe and can be re use for diferent authz context.
The only way to customize http headers (needed for authz or whatever other http headers) is to add some value in _meta such:

  McpSchema.CallToolRequest request = McpSchema.CallToolRequest.builder()
          .name("test_add")
          .arguments(args)
          .meta(Map.of("Authorization", "really complicated password"))
          .build();

  McpSchema.CallToolResult result = mcpSyncClient.callTool(request);

Then having a SyncHttpRequestCustomizer

  public static SyncHttpRequestCustomizer getRequestCustomizer() {
      return (builder, method, endpoint, body) -> {
          try {
              if (body != null) {
                  McpSchema.JSONRPCMessage jsonrpcMessage = McpSchema.deserializeJsonRpcMessage(OBJECT_MAPPER, body);
                  if (jsonrpcMessage instanceof McpSchema.JSONRPCRequest jsonrpcRequest
                          && jsonrpcRequest.params() instanceof Map params) {
                      Optional<Map<String, Object>> meta = Optional.ofNullable((Map) params.get("_meta"));
                      Optional<String> authz = Optional.ofNullable(
                              (String) meta.orElse(Collections.emptyMap()).get(AUTHORIZATION_HEADER));
                      authz.ifPresent(s -> builder.header(AUTHORIZATION_HEADER, s));
                  }
              }
          } catch (Throwable e) {
              throw new RuntimeException(e);
          }
      };
  }

it works but looks brittle as the headers are part of the body and it's not possible to customize header with webflux such WebClientStreamableHttpTransport

I was thinking of something such

McpSchema.CallToolResult result = mcpSyncClient.callTool(request, transportContext);

transportContext could be very similar to the one used for server part.

and changing the SyncHttpRequestCustomizer accordingly to accept this new arg? but what about WebClientStreamableHttpTransport.
I'm happy to help here if you tell me if you have any preference for implementing this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions