McpServer.connect() Appears to Return null/undefined with Per-Request Server Instantiation and await; Leads to "Method not found"
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 54/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- express, node.js, typescript
- Lĩnh vực
- backend-api-design
Hướng nghiên cứu
Start by reading the McpServer.connect() implementation and the StreamableHTTPServerTransport request handling code; the issue reports behavior with SDK versions 1.11.0 and 1.11.4, Node.js 20.16.0, and Express 4.19.2. Reproduce with the per request server and transport setup described in the issue, then compare synchronous connect() with the reported singleton setup. Done means identifying whether the return value or request listener is at fault and confirming the reported MCP request behavior.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Hello MCP SDK Team,
I'm encountering an issue where McpServer.connect(transport) appears to return null or undefined instead of a valid ServerConnection object when using StreamableHTTPServerTransport. This subsequently prevents the transport's request listener from being set, leading to "Method not found" errors for all MCP calls (including mcp.describe).
Environment:
@modelcontextprotocol/sdkVersion:1.11.4(also observed with1.11.0)- Node.js Version:
v20.16.0 - Operating System: macOS Sonoma 14.4 (Apple M1 Pro)
- Express.js Version:
4.19.2(Please update if different) - TypeScript Version:
5.4.5(Please update if different)
Problem Description:
In my application, which uses Express.js, I'm setting up an McpServer with StreamableHTTPServerTransport. The primary issue is that the synchronous call to server.connect(transport) seems to complete without throwing an immediate error, but the returned value assigned to our connection variable is observed to be null or undefined through logging.
Because the connection object is not valid:
- The
transport.setRequestListener(...)call, which normally happens insideMcpServer.connect(), is likely not being effectively executed for the transport instance. - When
transport.handleRequest(...)is later called,transport.requestListenerisundefined. - This results in either:
- A "Method not found" JSON-RPC error if
sessionIdGeneratorforStreamableHTTPServerTransportisundefined. - A "Bad Request: Server not initialized" JSON-RPC error (HTTP 400) if
sessionIdGeneratoris explicitly set (e.g., torandomUUID), astransport.handleRequesthas a specific check forthis.requestListener.
- A "Method not found" JSON-RPC error if
The core problem is that McpServer.connect() is not returning a valid ServerConnection object as expected by its type signature, thus breaking the link with the transport.
My Application Code Structure:
Here's a simplified representation of the relevant parts of my application code where the issue is observed.
1. src/mcp/mcpServer.ts:
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import { Application, Request, Response } from "express";
import logger from "../logger"; // My application's logger (e.g., Winston)
// import { randomUUID } from "crypto"; // If using randomUUID for sessionIdGenerator
async function getCountryList() {
return [
{ name: "United States", code: "US" },
{ name: "Canada", code: "CA" },
];
}
// Original problematic approach: Factory function called per request
function getServer(): McpServer {
const server = new McpServer({ name: "AppTools", version: "1.0.0" });
server.tool("country-list", "Returns a list of all countries in the system", {}, async () => {
const countries = await getCountryList();
return {
content: [
{
type: "resource",
resource: { mimeType: "application/json", text: JSON.stringify(countries) },
},
],
};
});
logger.info("Registered tool 'country-list' on new server instance.");
return server;
}
export function setupMCPServer(app: Application) {
app.post("/mcp", async (req: Request, res: Response) => {
try {
const server = getServer(); // Problem: New server instance on every request
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: undefined, // Also tested with randomUUID
});
logger.info("Connecting transport to server (per-request instance)...");
console.log("Connecting transport to server (per-request instance)..."); // For visibility
// Problem: 'await' on a synchronous function.
// This is where 'connection' effectively becomes null/undefined based on logs.
const connection = await server.connect(transport);
if (connection) {
logger.info("server.connect seemed to return a truthy connection object.");
console.log("server.connect seemed to return a truthy connection object.");
} else {
logger.error("CRITICAL: server.connect returned null or undefined.");
console.error("CRITICAL: server.connect returned null or undefined.");
}
logger.info("Transport supposedly connected to server.");
console.log("Transport supposedly connected to server.");
await transport.handleRequest(req, res, req.body);
logger.info("Request handled successfully by transport.");
console.log("Request handled successfully by transport.");
} catch (error: any) {
logger.error("Error handling MCP request:", { message: error.message, stack: error.stack });
console.error("Error handling MCP request:", error);
if (!res.headersSent) {
res.status(500).json({
jsonrpc: "2.0",
error: { code: -32603, message: "Internal server error" },
id: req.body?.id || null,
});
}
}
});
app.get("/mcp", (req: Request, res: Response) => {
res.status(405).json({ jsonrpc: "2.0", error: { code: -32000, message: "Method Not Allowed. Use POST." }, id: null });
});
app.delete("/mcp", (req: Request, res: Response) => {
res.status(405).json({ jsonrpc: "2.0", error: { code: -32000, message: "Method Not Allowed. Use POST." }, id: null });
});
logger.info("Original MCP routes configured.");
}
- src/app.ts (How setupMCPServer is called):
import express from "express";
import * as mcpServerSetup from "./mcp/mcpServer"; // Path to the mcpServer.ts above
import logger from "./logger"; // My application's logger
const app = express();
const port = process.env.API_PORT;
app.use(express.json());
try {
mcpServerSetup.setupMCPServer(app);
logger.info("Main MCP server setup completed.");
} catch (error) {
logger.error("Failed to set up main MCP server:", error);
process.exit(1);
}
app.listen(port, () => {
logger.info(`Main Server is running at http://localhost:${port}`);
logger.info(`Main MCP server available at http://localhost:${port}/mcp`);
});
- tsconfig.json:
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"rootDir": "./src",
"moduleResolution": "node",
"resolveJsonModule": true,
"allowJs": true,
"outDir": "./dist/",
"downlevelIteration": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": false,
"strictNullChecks": false,
"noImplicitReturns": false,
"skipLibCheck": true,
"types": ["./src/types/express"]
},
"exclude": ["node_modules"],
"include": ["src/**/*", "src/**/*.json"]
}
- package.json (Key Dependencies - please specify your exact versions):
{
"name": "my-application",
"version": "1.0.0",
"dependencies": {
"@modelcontextprotocol/sdk": "^1.11.4",
"express": "^4.19.2",
"typescript": "^5.4.5"
}
}
Steps to Reproduce (Using the application structure above):
Set up the project with the described files and relevant dependencies.
Compile TypeScript.
Start the server.
Send a POST request:
curl -v -X POST http://localhost:3777/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc":"2.0","id":"test-country","method":"AppTools.country-list","params":{}}'
Or for mcp.describe:
curl -v -X POST http://localhost:3777/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc":"2.0","id":"test-describe","method":"mcp.describe","params":{}}'
Observed Behavior:
The curl command receives a "Method not found" error:
{"jsonrpc":"2.0","id":"test-country","error":{"code":-32601,"message":"Method not found"}}
Server logs show the CRITICAL: server.connect returned null or undefined. message (or similar if connection logging is added).
Expected Behavior:
McpServer.connect(transport) should return a valid ServerConnection object. The curl command should successfully invoke the AppTools.country-list method or mcp.describe.
Debugging Notes & Attempts:
We have confirmed that McpServer.connect is a synchronous method, so the await in my original code is incorrect.
Even when correcting my application structure to use a singleton McpServer instance and calling server.connect(transport) synchronously (as demonstrated in separate minimal tests), the core issue of connect() returning null/undefined persisted. This suggests the problem is not solely due to my initial incorrect usage patterns but might indicate a deeper issue within the SDK's connect logic when interacting with StreamableHTTPServerTransport.
Could you please investigate why McpServer.connect() might be failing to establish a proper connection and return null/undefined in this scenario with StreamableHTTPServerTransport?
Thank you for your time and assistance!
- Ngôn ngữ chính
- TypeScript
- Star
- 13.8k
- Fork
- 2.5k
- Merge trung bình
- 6 ngày 23 giờ
- Pull request đã merge (30 ngày)
- 2
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của modelcontextprotocol/typescript-sdk
-
v2
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 72/100
modelcontextprotocol/typescript-sdk#3270 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`callTool` with `allowInputRequired: true` throws on an `input_required` result when the tool declares an `outputSchema`Có thể đã có người làm @mgoldsborough đã nhận 6 ngày trước. Đang mởv2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
modelcontextprotocol/typescript-sdk#3249 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
modelcontextprotocol/typescript-sdk#3243 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
TypeScript client accepts an unknown `resultType` in the first `server/discover` replyCó thể đã có người làm @Tidehyc đã nhận 8 ngày trước. Đang mởv2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
modelcontextprotocol/typescript-sdk#3235 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
UriTemplate.match() throws a TypeError on an empty expression it acceptsCó thể đã có người làm @yunusemrejs đã nhận 12 ngày trước. Đang mởv1 v2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
modelcontextprotocol/typescript-sdk#3221 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của modelcontextprotocol/typescript-sdk
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Vessel9817/onion-site-template#512 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
nightly-tests
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
BlackBeltTechnology/pi-agent-dashboard#1256 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
approved
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
fritzprix/libr-agent#3266 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
better-auth/better-auth#13101 · 1 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày