Report SSE 401 errors to the client
This commit is contained in:
parent
14db05c2a2
commit
8bb5308797
2 changed files with 24 additions and 1 deletions
11
server/src/errors.ts
Normal file
11
server/src/errors.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
export interface SseError extends Error {
|
||||
code: number;
|
||||
}
|
||||
|
||||
export function isSseError(error: unknown): error is SseError {
|
||||
if (!(error instanceof Error)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return "code" in error && typeof error.code === "number";
|
||||
}
|
||||
|
|
@ -12,6 +12,7 @@ import {
|
|||
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
|
||||
import express from "express";
|
||||
import { findActualExecutable } from "spawn-rx";
|
||||
import { isSseError } from "./errors.js";
|
||||
import mcpProxy from "./mcpProxy.js";
|
||||
|
||||
const SSE_HEADERS_PASSTHROUGH = ['Authorization'];
|
||||
|
|
@ -98,7 +99,18 @@ app.get("/sse", async (req, res) => {
|
|||
try {
|
||||
console.log("New SSE connection");
|
||||
|
||||
const backingServerTransport = await createTransport(req);
|
||||
let backingServerTransport;
|
||||
try {
|
||||
backingServerTransport = await createTransport(req);
|
||||
} catch (error) {
|
||||
if (isSseError(error) && error.code === 401) {
|
||||
console.error("Received 401 Unauthorized from MCP server:", error.message);
|
||||
res.status(401).json(error);
|
||||
return;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
console.log("Connected MCP client to backing server transport");
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue