fastmcp: "Server did not connect" error

I tried https://github.com/punkpeye/fastmcp/blob/main/src/examples/addition.ts but changing

server.start({
  transportType: "stdio",
});

to

server.start({
  transportType: "sse",
  sse: {
    endpoint: "/ca",
    port: 8080,
  },
});

then when using npx fastmcp inspect to debug it I always got below error.

node_modules/fastmcp/dist/FastMCP.js:251
      throw new UnexpectedStateError("Server did not connect");

I debugged the source, and found out that error is happened at https://github.com/punkpeye/fastmcp/blob/main/src/FastMCP.ts#L554-L556 and the reason is clientCapabilities is empty, because though https://github.com/punkpeye/fastmcp/blob/main/src/FastMCP.ts#L542-L552 seems to be waiting for https://github.com/modelcontextprotocol/typescript-sdk/blob/main/src/server/index.ts#L246-L261 to execute, after 10 times trying, if _oninitialize is still not invoked, the error happens.

I’m not sure why _oninitialize isn’t invoke quickly enough to prevent the error. But I’m not doing anything special here.

And I also tried original mcp server as below https://github.com/modelcontextprotocol/servers/blob/main/src/everything/sse.ts it just works.

I’m not sure it’s a bug, or I didn’t do it the correct way. I’ll appreciate if you could give me some help.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 17 (7 by maintainers)

Most upvoted comments

Since this was causing a lot of confusion, I refactored the code to log warning instead of throwing an error.

https://github.com/punkpeye/fastmcp/releases/tag/v1.18.0

@alexgorbatchev The server is not expected to start without inspector/actual client.

That’s because the initialization phase depends on getting the client capabilities.

So since you are not starting with a client… it cannot.

I have the same problem with my Clever Cloud Documentation MCP server:

$ npm run dev

> clever-doc-mcp-server@1.0.0 dev
> tsx src/index.ts

/Users/horacio/git/llm/clever-doc-mcp-server/node_modules/fastmcp/src/FastMCP.ts:555
      throw new UnexpectedStateError("Server did not connect");
            ^


UnexpectedStateError: Server did not connect
    at FastMCPSession.connect (/Users/horacio/git/llm/clever-doc-mcp-server/node_modules/fastmcp/src/FastMCP.ts:555:13)
    at FastMCP.start (/Users/horacio/git/llm/clever-doc-mcp-server/node_modules/fastmcp/src/FastMCP.ts:1084:7) {
  extras: undefined
}

Node.js v21.7.1

or

$ npm run start

> clever-doc-mcp-server@1.0.0 start
> node dist/index.js

Fatal error in main(): UnexpectedStateError: Server did not connect
    at FastMCPSession.connect (file:///Users/horacio/git/llm/clever-doc-mcp-server/node_modules/fastmcp/dist/FastMCP.js:251:13)
    at async FastMCP.start (file:///Users/horacio/git/llm/clever-doc-mcp-server/node_modules/fastmcp/dist/FastMCP.js:691:7)
    at async main (file:///Users/horacio/git/llm/clever-doc-mcp-server/dist/index.js:72:5) {
  extras: undefined
}

It seems the problem happens with FastMCP in all versions from 1.8.0, and it works in versions 1.7.0 and lower.

The same problem happens to piapi-mcp-server, and with a simple project using the example in the Quickstarter section.