Skip to content

Commit 24c1ee2

Browse files
committed
feat: enable cors
1 parent 1b6c40d commit 24c1ee2

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
A TypeScript SSE proxy for [MCP](https://modelcontextprotocol.io/) servers that use `stdio` transport.
44

5+
> [!NOTE]
6+
> CORS is enabled by default.
7+
58
> [!NOTE]
69
> For a Python implementation, see [mcp-proxy](https://github.com/sparfenyuk/mcp-proxy).
710

src/MCPProxy.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,25 @@ export const startSSEServer = async <T extends ServerLike>({
193193
* @author https://dev.classmethod.jp/articles/mcp-sse/
194194
*/
195195
const httpServer = http.createServer(async (req, res) => {
196+
if (req.headers.origin) {
197+
try {
198+
const origin = new URL(req.headers.origin);
199+
200+
res.setHeader("Access-Control-Allow-Origin", origin.origin);
201+
res.setHeader("Access-Control-Allow-Credentials", "true");
202+
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
203+
res.setHeader("Access-Control-Allow-Headers", "*");
204+
} catch (error) {
205+
console.error("Error parsing origin:", error);
206+
}
207+
}
208+
209+
if (req.method === "OPTIONS") {
210+
res.writeHead(204);
211+
res.end();
212+
return;
213+
}
214+
196215
if (req.method === "GET" && req.url === `/ping`) {
197216
res.writeHead(200).end("pong");
198217

0 commit comments

Comments
 (0)