Skip to content

How to propagate the headers received by a Server to a Client #373

@aleksousa

Description

@aleksousa

Hello everyone. I’m developing a gateway for the company where I work, in which we will centralize several security rules, auditing, etc., so that this does not need to be replicated in different MCP applications. In this way, the gateway will have multiple servers and clients started, one for each destination.

The difficulty I’m having is using the headers received by a server at the moment of executing a client’s tool. Each path of my gateway points to a different Server and Client, so when the Server receives a request, it applies the validations and uses the client to call the tool, but because everything was started at startup, the request sent to the destination MCP will lack the necessary headers. Only the parameters are propagated, but I can’t pass the headers.

I’m trying something like this, but I’ve already tried using middleware and other approaches.

client := mcp.NewClient(&mcp.Implementation{Name: "A"}, &mcp.ClientOptions{})
clientSession, err := client.Connect(ctx, &mcp.StreamableClientTransport{....}, nil)
server := mcp.NewServer(&mcp.Implementation{...}, nil)

tools := clientSession.Tools(ctx, nil)
for tool, _ := range tools {
     server.AddTool(tool, func(ctx context.Context, req *mcp.CallToolRequest) (*.mcp.CallToolResult, error) {
          // GET HEADERS
          return clientSession.CallTool(ctx, req.Params)
     ))
}
...

Is there a way to do 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