Skip to content

Expose request context access in McpServerExchange implementations #435

@gakkiyomi

Description

@gakkiyomi

How can I retrieve information carried in the request (such as headers and client IP) from McpSyncServerExchange or McpAsyncServerExchange? I'm using HttpServletSseServerTransportProvider and need to log client IPs calling MCP services while monitoring traffic. Currently I modified the 0.10.0 SDK source as follows: Made McpAsyncServerExchange.getSession() public, McpServerSession.getTransport() public, and HttpServletMcpSessionTransport.getAsyncContext() public to implement:

/**
 * Retrieves client IP from exchange
 * @param exchange {@link McpSyncServerExchange}
 * @return IP address
 */
private String getClientIp(McpSyncServerExchange exchange) {
    var session = exchange.getExchange().getSession();
    var transport = (HttpServletMcpSessionTransport) session.getTransport();
    var context = transport.getAsyncContext();
    var request = (HttpServletRequest) context.getRequest();
    return NetUtils.getClientIpAddress(request);
}

But this approach feels hacky ("Monkey patch"). Are there plans to expose request context access during server-side processing in future versions?

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