Skip to content

MCP server to be able to return custom http codes #737

@poornimas-iyer

Description

@poornimas-iyer

Please do a quick search on GitHub issues first, the feature you are about to request might have already been requested.

I am writing a custom MCP server. I am using an existing HttpServletStatelessServerTransport but mine is a stateful server with multiple pods. But since I do not have control on the session Id generation & validation & moreover, the validation in stateful transport is based on memory, I have chosen a stateless transport. But now on validating the sessionId in every MCP request, I want to be able to return an 404, if session id is not found or return 500 or other status codes. But I do not have this control now.

I was looking if there is a way in HttpServletStatelessServerTransport by which the MCP Error message can be passed by the McpStatelessServerHandler in the exception thrown by it

Current Behavior
In the below code, we can see that even if handleRequest method of McpStatelessServerHandler throws IllegalArgumentException, a customized error message cannot be returned to the client.

			logger.error("Failed to deserialize message: {}", e.getMessage());
			this.responseError(response, HttpServletResponse.SC_BAD_REQUEST, new McpError("Invalid message format"));
		}
		catch (Exception e) {
			logger.error("Unexpected error handling message: {}", e.getMessage());
			this.responseError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
					new McpError("Unexpected error: " + e.getMessage()));
		}```

**Context**

The only work around is to find the MCP request method before calling the transport's service method and return appropriate http code with required message. This is not ideal though

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