Report SSE 401 errors to the client

This commit is contained in:
Justin Spahr-Summers
2025-01-24 11:04:44 +00:00
parent 14db05c2a2
commit 8bb5308797
2 changed files with 24 additions and 1 deletions

11
server/src/errors.ts Normal file
View 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";
}

View File

@@ -12,6 +12,7 @@ import {
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js"; import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
import express from "express"; import express from "express";
import { findActualExecutable } from "spawn-rx"; import { findActualExecutable } from "spawn-rx";
import { isSseError } from "./errors.js";
import mcpProxy from "./mcpProxy.js"; import mcpProxy from "./mcpProxy.js";
const SSE_HEADERS_PASSTHROUGH = ['Authorization']; const SSE_HEADERS_PASSTHROUGH = ['Authorization'];
@@ -98,7 +99,18 @@ app.get("/sse", async (req, res) => {
try { try {
console.log("New SSE connection"); 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"); console.log("Connected MCP client to backing server transport");