mirror of
https://github.com/upstash/context7.git
synced 2025-07-03 07:04:25 +00:00
refactor: replace url.parse with URL API for session ID extraction and validate transports
This commit is contained in:
parent
2e6c9a6c6b
commit
4f04e02851
15
src/index.ts
15
src/index.ts
@ -9,7 +9,6 @@ import { SearchResponse } from "./lib/types.js";
|
|||||||
import { createServer } from "http";
|
import { createServer } from "http";
|
||||||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
||||||
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
|
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
|
||||||
import { parse } from "url";
|
|
||||||
import { Command } from "commander";
|
import { Command } from "commander";
|
||||||
|
|
||||||
const DEFAULT_MINIMUM_TOKENS = 10000;
|
const DEFAULT_MINIMUM_TOKENS = 10000;
|
||||||
@ -26,6 +25,15 @@ const cliOptions = program.opts<{
|
|||||||
port: string;
|
port: string;
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
|
// Validate transport option
|
||||||
|
const allowedTransports = ["stdio", "http", "sse"];
|
||||||
|
if (!allowedTransports.includes(cliOptions.transport)) {
|
||||||
|
console.error(
|
||||||
|
`Invalid --transport value: '${cliOptions.transport}'. Must be one of: stdio, http, sse.`
|
||||||
|
);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
// Transport configuration
|
// Transport configuration
|
||||||
const TRANSPORT_TYPE = (cliOptions.transport || "stdio") as "stdio" | "http" | "sse";
|
const TRANSPORT_TYPE = (cliOptions.transport || "stdio") as "stdio" | "http" | "sse";
|
||||||
|
|
||||||
@ -217,8 +225,9 @@ async function main() {
|
|||||||
await requestServer.connect(sseTransport);
|
await requestServer.connect(sseTransport);
|
||||||
} else if (url === "/messages" && req.method === "POST") {
|
} else if (url === "/messages" && req.method === "POST") {
|
||||||
// Get session ID from query parameters
|
// Get session ID from query parameters
|
||||||
const parsedUrl = parse(req.url || "", true);
|
const sessionId =
|
||||||
const sessionId = parsedUrl.query.sessionId as string;
|
new URL(req.url || "", `http://${req.headers.host}`).searchParams.get("sessionId") ??
|
||||||
|
"";
|
||||||
|
|
||||||
if (!sessionId) {
|
if (!sessionId) {
|
||||||
res.writeHead(400);
|
res.writeHead(400);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user