Linting
This commit is contained in:
parent
50df0e1ec4
commit
3207959af0
3 changed files with 296 additions and 236 deletions
|
|
@ -349,7 +349,7 @@ const App = () => {
|
|||
const headers: HeadersInit = {};
|
||||
const proxyAuthToken = getMCPProxyAuthToken(config);
|
||||
if (proxyAuthToken) {
|
||||
headers['Authorization'] = `Bearer ${proxyAuthToken}`;
|
||||
headers["Authorization"] = `Bearer ${proxyAuthToken}`;
|
||||
}
|
||||
|
||||
fetch(`${getMCPProxyAddress(config)}/config`, { headers })
|
||||
|
|
|
|||
|
|
@ -246,7 +246,7 @@ export function useConnection({
|
|||
const proxyAuthToken = getMCPProxyAuthToken(config);
|
||||
const headers: HeadersInit = {};
|
||||
if (proxyAuthToken) {
|
||||
headers['Authorization'] = `Bearer ${proxyAuthToken}`;
|
||||
headers["Authorization"] = `Bearer ${proxyAuthToken}`;
|
||||
}
|
||||
const proxyHealthResponse = await fetch(proxyHealthUrl, { headers });
|
||||
const proxyHealth = await proxyHealthResponse.json();
|
||||
|
|
@ -335,7 +335,7 @@ export function useConnection({
|
|||
const proxyAuthToken = getMCPProxyAuthToken(config);
|
||||
const proxyHeaders: HeadersInit = {};
|
||||
if (proxyAuthToken) {
|
||||
proxyHeaders['Authorization'] = `Bearer ${proxyAuthToken}`;
|
||||
proxyHeaders["Authorization"] = `Bearer ${proxyAuthToken}`;
|
||||
}
|
||||
|
||||
// Create appropriate transport
|
||||
|
|
@ -356,7 +356,11 @@ export function useConnection({
|
|||
fetch: (
|
||||
url: string | URL | globalThis.Request,
|
||||
init: RequestInit | undefined,
|
||||
) => fetch(url, { ...init, headers: { ...headers, ...proxyHeaders } }),
|
||||
) =>
|
||||
fetch(url, {
|
||||
...init,
|
||||
headers: { ...headers, ...proxyHeaders },
|
||||
}),
|
||||
},
|
||||
requestInit: {
|
||||
headers: { ...headers, ...proxyHeaders },
|
||||
|
|
@ -372,7 +376,11 @@ export function useConnection({
|
|||
fetch: (
|
||||
url: string | URL | globalThis.Request,
|
||||
init: RequestInit | undefined,
|
||||
) => fetch(url, { ...init, headers: { ...headers, ...proxyHeaders } }),
|
||||
) =>
|
||||
fetch(url, {
|
||||
...init,
|
||||
headers: { ...headers, ...proxyHeaders },
|
||||
}),
|
||||
},
|
||||
requestInit: {
|
||||
headers: { ...headers, ...proxyHeaders },
|
||||
|
|
@ -388,7 +396,11 @@ export function useConnection({
|
|||
fetch: (
|
||||
url: string | URL | globalThis.Request,
|
||||
init: RequestInit | undefined,
|
||||
) => fetch(url, { ...init, headers: { ...headers, ...proxyHeaders } }),
|
||||
) =>
|
||||
fetch(url, {
|
||||
...init,
|
||||
headers: { ...headers, ...proxyHeaders },
|
||||
}),
|
||||
},
|
||||
requestInit: {
|
||||
headers: { ...headers, ...proxyHeaders },
|
||||
|
|
@ -471,7 +483,8 @@ export function useConnection({
|
|||
if (isProxyAuthError(error)) {
|
||||
toast({
|
||||
title: "Proxy Authentication Required",
|
||||
description: "Please enter the session token from the proxy server console in the Configuration settings.",
|
||||
description:
|
||||
"Please enter the session token from the proxy server console in the Configuration settings.",
|
||||
variant: "destructive",
|
||||
});
|
||||
setConnectionStatus("error");
|
||||
|
|
|
|||
|
|
@ -89,33 +89,43 @@ app.use((req, res, next) => {
|
|||
const webAppTransports: Map<string, Transport> = new Map<string, Transport>(); // Web app transports by web app sessionId
|
||||
const serverTransports: Map<string, Transport> = new Map<string, Transport>(); // Server Transports by web app sessionId
|
||||
|
||||
const sessionToken = randomBytes(32).toString('hex');
|
||||
const sessionToken = randomBytes(32).toString("hex");
|
||||
const authDisabled = !!process.env.DANGEROUSLY_OMIT_AUTH;
|
||||
|
||||
// Origin validation middleware to prevent DNS rebinding attacks
|
||||
const originValidationMiddleware = (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
const originValidationMiddleware = (
|
||||
req: express.Request,
|
||||
res: express.Response,
|
||||
next: express.NextFunction,
|
||||
) => {
|
||||
const origin = req.headers.origin;
|
||||
|
||||
// Default origins based on CLIENT_PORT or use environment variable
|
||||
const clientPort = process.env.CLIENT_PORT || '6274';
|
||||
const clientPort = process.env.CLIENT_PORT || "6274";
|
||||
const defaultOrigins = [
|
||||
`http://localhost:${clientPort}`,
|
||||
`http://127.0.0.1:${clientPort}`
|
||||
`http://127.0.0.1:${clientPort}`,
|
||||
];
|
||||
const allowedOrigins = process.env.ALLOWED_ORIGINS?.split(',') || defaultOrigins;
|
||||
const allowedOrigins =
|
||||
process.env.ALLOWED_ORIGINS?.split(",") || defaultOrigins;
|
||||
|
||||
if (origin && !allowedOrigins.includes(origin)) {
|
||||
console.error(`Invalid origin: ${origin}`);
|
||||
res.status(403).json({
|
||||
error: 'Forbidden - invalid origin',
|
||||
message: 'Request blocked to prevent DNS rebinding attacks. Configure allowed origins via environment variable.'
|
||||
error: "Forbidden - invalid origin",
|
||||
message:
|
||||
"Request blocked to prevent DNS rebinding attacks. Configure allowed origins via environment variable.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
next();
|
||||
};
|
||||
|
||||
const authMiddleware = (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
const authMiddleware = (
|
||||
req: express.Request,
|
||||
res: express.Response,
|
||||
next: express.NextFunction,
|
||||
) => {
|
||||
if (authDisabled) {
|
||||
return next();
|
||||
}
|
||||
|
|
@ -123,12 +133,13 @@ const authMiddleware = (req: express.Request, res: express.Response, next: expre
|
|||
const sendUnauthorized = () => {
|
||||
res.status(401).json({
|
||||
error: "Unauthorized",
|
||||
message: "Authentication required. Use the session token shown in the console when starting the server."
|
||||
message:
|
||||
"Authentication required. Use the session token shown in the console when starting the server.",
|
||||
});
|
||||
};
|
||||
|
||||
const authHeader = req.headers.authorization;
|
||||
if (!authHeader || !authHeader.startsWith('Bearer ')) {
|
||||
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
||||
sendUnauthorized();
|
||||
return;
|
||||
}
|
||||
|
|
@ -216,7 +227,11 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
|
|||
}
|
||||
};
|
||||
|
||||
app.get("/mcp", originValidationMiddleware, authMiddleware, async (req, res) => {
|
||||
app.get(
|
||||
"/mcp",
|
||||
originValidationMiddleware,
|
||||
authMiddleware,
|
||||
async (req, res) => {
|
||||
const sessionId = req.headers["mcp-session-id"] as string;
|
||||
console.log(`Received GET message for sessionId ${sessionId}`);
|
||||
try {
|
||||
|
|
@ -233,9 +248,14 @@ app.get("/mcp", originValidationMiddleware, authMiddleware, async (req, res) =>
|
|||
console.error("Error in /mcp route:", error);
|
||||
res.status(500).json(error);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
app.post("/mcp", originValidationMiddleware, authMiddleware, async (req, res) => {
|
||||
app.post(
|
||||
"/mcp",
|
||||
originValidationMiddleware,
|
||||
authMiddleware,
|
||||
async (req, res) => {
|
||||
const sessionId = req.headers["mcp-session-id"] as string | undefined;
|
||||
let serverTransport: Transport | undefined;
|
||||
if (!sessionId) {
|
||||
|
|
@ -303,9 +323,14 @@ app.post("/mcp", originValidationMiddleware, authMiddleware, async (req, res) =>
|
|||
res.status(500).json(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
app.delete("/mcp", originValidationMiddleware, authMiddleware, async (req, res) => {
|
||||
app.delete(
|
||||
"/mcp",
|
||||
originValidationMiddleware,
|
||||
authMiddleware,
|
||||
async (req, res) => {
|
||||
const sessionId = req.headers["mcp-session-id"] as string | undefined;
|
||||
console.log(`Received DELETE message for sessionId ${sessionId}`);
|
||||
let serverTransport: Transport | undefined;
|
||||
|
|
@ -330,9 +355,14 @@ app.delete("/mcp", originValidationMiddleware, authMiddleware, async (req, res)
|
|||
res.status(500).json(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
app.get("/stdio", originValidationMiddleware, authMiddleware, async (req, res) => {
|
||||
app.get(
|
||||
"/stdio",
|
||||
originValidationMiddleware,
|
||||
authMiddleware,
|
||||
async (req, res) => {
|
||||
try {
|
||||
console.log("New STDIO connection request");
|
||||
let serverTransport: Transport | undefined;
|
||||
|
|
@ -392,9 +422,14 @@ app.get("/stdio", originValidationMiddleware, authMiddleware, async (req, res) =
|
|||
console.error("Error in /stdio route:", error);
|
||||
res.status(500).json(error);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
app.get("/sse", originValidationMiddleware, authMiddleware, async (req, res) => {
|
||||
app.get(
|
||||
"/sse",
|
||||
originValidationMiddleware,
|
||||
authMiddleware,
|
||||
async (req, res) => {
|
||||
try {
|
||||
console.log(
|
||||
"New SSE connection request. NOTE: The sse transport is deprecated and has been replaced by StreamableHttp",
|
||||
|
|
@ -441,9 +476,14 @@ app.get("/sse", originValidationMiddleware, authMiddleware, async (req, res) =>
|
|||
console.error("Error in /sse route:", error);
|
||||
res.status(500).json(error);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
app.post("/message", originValidationMiddleware, authMiddleware, async (req, res) => {
|
||||
app.post(
|
||||
"/message",
|
||||
originValidationMiddleware,
|
||||
authMiddleware,
|
||||
async (req, res) => {
|
||||
try {
|
||||
const sessionId = req.query.sessionId;
|
||||
console.log(`Received POST message for sessionId ${sessionId}`);
|
||||
|
|
@ -460,7 +500,8 @@ app.post("/message", originValidationMiddleware, authMiddleware, async (req, res
|
|||
console.error("Error in /message route:", error);
|
||||
res.status(500).json(error);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
app.get("/health", (req, res) => {
|
||||
res.json({
|
||||
|
|
@ -481,22 +522,28 @@ app.get("/config", originValidationMiddleware, authMiddleware, (req, res) => {
|
|||
}
|
||||
});
|
||||
|
||||
const PORT = parseInt(process.env.PORT || '6277', 10);
|
||||
const HOST = process.env.HOST || '127.0.0.1';
|
||||
const PORT = parseInt(process.env.PORT || "6277", 10);
|
||||
const HOST = process.env.HOST || "127.0.0.1";
|
||||
|
||||
const server = app.listen(PORT, HOST);
|
||||
server.on("listening", () => {
|
||||
console.log(`⚙️ Proxy server listening on ${HOST}:${PORT}`);
|
||||
if (!authDisabled) {
|
||||
console.log(`🔑 Session token: ${sessionToken}`);
|
||||
console.log(`Use this token to authenticate requests or set DANGEROUSLY_OMIT_AUTH=true to disable auth`);
|
||||
console.log(
|
||||
`Use this token to authenticate requests or set DANGEROUSLY_OMIT_AUTH=true to disable auth`,
|
||||
);
|
||||
|
||||
// Display clickable URL with pre-filled token
|
||||
const clientPort = process.env.CLIENT_PORT || '6274';
|
||||
const clientPort = process.env.CLIENT_PORT || "6274";
|
||||
const clientUrl = `http://localhost:${clientPort}/?MCP_PROXY_AUTH_TOKEN=${sessionToken}`;
|
||||
console.log(`\n🔗 Open inspector with token pre-filled:\n ${clientUrl}\n (Auto-open is disabled when authentication is enabled)\n`);
|
||||
console.log(
|
||||
`\n🔗 Open inspector with token pre-filled:\n ${clientUrl}\n (Auto-open is disabled when authentication is enabled)\n`,
|
||||
);
|
||||
} else {
|
||||
console.log(`⚠️ WARNING: Authentication is disabled. This is not recommended.`);
|
||||
console.log(
|
||||
`⚠️ WARNING: Authentication is disabled. This is not recommended.`,
|
||||
);
|
||||
}
|
||||
});
|
||||
server.on("error", (err) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue