cloudflare / cloudflare/workers-mcp

init hook rejects with “Cannot register capabilities after connecting to transport” when awaiting async code

Open
#26 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
647
Forks
67
PR merge metrics
No merged PRs in 30d

Description

### Environment

| Package | Version |
|------------------------|---------|
| workers-mcp | 0.1.0-3 |
| @modelcontextprotocol/sdk | 1.12.1 |

### Steps to reproduce it
* Add code that depends on promises in the init hook

```typescript
export class AuthenticatedMCP extends DurableMCP {
server = new McpServer({
name: 'My MCP Server',
version: '1.0.0'
});

async init() {
await registerTestPrompt(this.server);
}
}

async function registerTestPrompt(server: McpServer): Promise {
await new Promise(r => setTimeout(r, 1000));
server.prompt(
'TestPrompt',
'A simple test prompt.',
{
testArg: z.string().describe('A test argument'),
},
async (request) => {
return {
messages: [
{
role: 'user',
content: {
type: 'text',
text: `You said: ${request.testArg}`,
},
},
],
};
}
);
};
```

### Expected behavior
`init` can await arbitrary promises before starting to add prompts to the server

### Actual behavior
The code raises this error, and the prompts are not registered

```
✘ [ERROR] Uncaught (in promise) Error: Cannot register capabilities after connecting to transport
at registerCapabilities
(file:////node_modules/@modelcontextprotocol/sdk/src/server/index.ts:114:13)
at setPromptRequestHandlers
(file:////node_modules/@modelcontextprotocol/sdk/src/server/mcp.ts:452:17)
at prompt
(file:////node_modules/@modelcontextprotocol/sdk/src/server/mcp.ts:877:10)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.