fix(examples): return 404 for unknown session IDs, 400 for missing (#1770)
This commit is contained in:
parent
d6a02c85c0
commit
8822c96315
2 changed files with 147 additions and 60 deletions
|
|
@ -327,13 +327,17 @@ async function startServer() {
|
|||
await mcpServer.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
return;
|
||||
} else if (sessionId) {
|
||||
res.status(404).json({
|
||||
jsonrpc: '2.0',
|
||||
error: { code: -32_001, message: 'Session not found' },
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
res.status(400).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32_000,
|
||||
message: 'Invalid or missing session ID'
|
||||
},
|
||||
error: { code: -32_000, message: 'Bad Request: Session ID required' },
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
|
|
@ -359,8 +363,12 @@ async function startServer() {
|
|||
app.get('/mcp', bearerAuth, async (req: Request, res: Response) => {
|
||||
const sessionId = req.headers['mcp-session-id'] as string | undefined;
|
||||
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
if (!sessionId) {
|
||||
res.status(400).send('Missing session ID');
|
||||
return;
|
||||
}
|
||||
if (!transports[sessionId]) {
|
||||
res.status(404).send('Session not found');
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -381,8 +389,12 @@ async function startServer() {
|
|||
app.delete('/mcp', bearerAuth, async (req: Request, res: Response) => {
|
||||
const sessionId = req.headers['mcp-session-id'] as string | undefined;
|
||||
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
if (!sessionId) {
|
||||
res.status(400).send('Missing session ID');
|
||||
return;
|
||||
}
|
||||
if (!transports[sessionId]) {
|
||||
res.status(404).send('Session not found');
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -929,13 +929,17 @@ app.post('/mcp', async (req: Request, res: Response) => {
|
|||
await mcpServer.connect(transport);
|
||||
await transport.handleRequest(req, res, req.body);
|
||||
return;
|
||||
} else if (sessionId) {
|
||||
res.status(404).json({
|
||||
jsonrpc: '2.0',
|
||||
error: { code: -32_001, message: 'Session not found' },
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
res.status(400).json({
|
||||
jsonrpc: '2.0',
|
||||
error: {
|
||||
code: -32_000,
|
||||
message: 'Invalid or missing session ID'
|
||||
},
|
||||
error: { code: -32_000, message: 'Bad Request: Session ID required' },
|
||||
id: null
|
||||
});
|
||||
return;
|
||||
|
|
@ -961,8 +965,12 @@ app.post('/mcp', async (req: Request, res: Response) => {
|
|||
app.get('/mcp', async (req: Request, res: Response) => {
|
||||
const sessionId = req.headers['mcp-session-id'] as string | undefined;
|
||||
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
if (!sessionId) {
|
||||
res.status(400).send('Missing session ID');
|
||||
return;
|
||||
}
|
||||
if (!transports[sessionId]) {
|
||||
res.status(404).send('Session not found');
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -988,8 +996,12 @@ app.get('/mcp', async (req: Request, res: Response) => {
|
|||
app.delete('/mcp', async (req: Request, res: Response) => {
|
||||
const sessionId = req.headers['mcp-session-id'] as string | undefined;
|
||||
|
||||
if (!sessionId || !transports[sessionId]) {
|
||||
res.status(400).send('Invalid or missing session ID');
|
||||
if (!sessionId) {
|
||||
res.status(400).send('Missing session ID');
|
||||
return;
|
||||
}
|
||||
if (!transports[sessionId]) {
|
||||
res.status(404).send('Session not found');
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue