Skip to content

fix: [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent … #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ycjcl868
Copy link

@ycjcl868 ycjcl868 commented Apr 10, 2025

…to the client

https://github.com/modelcontextprotocol/typescript-sdk/blob/c6668c1f807745a8ae7434702fcbad100c332d92/src/server/sse.ts#L86

Based on the implementation of handlePostMessage, it executes when there is no established SSE connection.

res.writeHead(500).end(message);
throw new Error(message);

According to the handling logic in server/src/index.ts, it is executed in the catch block:

console.error("Error in /message route:", error);
res.status(500).json(error);

This causes the server service to crash directly.

Motivation and Context

How Has This Been Tested?

Breaking Changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Sorry, something went wrong.

@cliffhall
Copy link
Contributor

cliffhall commented Apr 10, 2025

Hi @ycjcl868!

How does adding an error handler fix the problem?

I believe this has been fixed and will be out in the next version of the Inspector. Can you duplicate it with what is on main? If so please amend your PR with steps to duplicate and screenshots.

I was getting it when troubleshooting #273 (comment) and that has been fixed with #290. Haven't seen it since.

@ycjcl868
Copy link
Author

https://github.com/modelcontextprotocol/typescript-sdk/blob/c6668c1f807745a8ae7434702fcbad100c332d92/src/server/sse.ts#L86

Based on the implementation of handlePostMessage, it executes when there is no established SSE connection.

res.writeHead(500).end(message);
throw new Error(message);

According to the handling logic in server/src/index.ts, it is executed in the catch block:

console.error("Error in /message route:", error);
res.status(500).json(error);

This causes the server service to crash directly.

@cliffhall
Copy link
Contributor

@ycjcl868 Can you post exact steps to reproduce, along with which server you are running?

@cliffhall cliffhall added the waiting on submitter Waiting for the submitter to provide more info label Apr 11, 2025
@cliffhall
Copy link
Contributor

cliffhall commented Apr 24, 2025

https://github.com/modelcontextprotocol/typescript-sdk/blob/c6668c1f807745a8ae7434702fcbad100c332d92/src/server/sse.ts#L86

Based on the implementation of handlePostMessage, it executes when there is no established SSE connection.

res.writeHead(500).end(message);
throw new Error(message);

According to the handling logic in server/src/index.ts, it is executed in the catch block:

console.error("Error in /message route:", error);
res.status(500).json(error);

This causes the server service to crash directly.

@ycjcl868 Could you provide a video or set of screenshots showing how this behaves with and without your fix?

Using res.status(500) in a server-side response does not cause the server to stop. It simply indicates that an error occurred during processing the request, and the server is sending an HTTP 500 status code to the client. The server should continue running and handling other requests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting on submitter Waiting for the submitter to provide more info
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants