93 lines
2.5 KiB
TypeScript
93 lines
2.5 KiB
TypeScript
/**
|
|
* Entry point for running the MCP server.
|
|
* Run with: npx mcp-shadertoy-server
|
|
* Or: node dist/index.js [--stdio]
|
|
*/
|
|
|
|
import { createMcpExpressApp } from "@modelcontextprotocol/express";
|
|
import { NodeStreamableHTTPServerTransport } from "@modelcontextprotocol/node";
|
|
import type { McpServer } from "@modelcontextprotocol/server";
|
|
import { StdioServerTransport } from "@modelcontextprotocol/server/stdio";
|
|
import cors from "cors";
|
|
import type { Request, Response } from "express";
|
|
import { createServer } from "./server.js";
|
|
|
|
/**
|
|
* Starts an MCP server with Streamable HTTP transport in stateless mode.
|
|
*
|
|
* @param createServer - Factory function that creates a new McpServer instance per request.
|
|
*/
|
|
export async function startStreamableHTTPServer(
|
|
createServer: () => McpServer,
|
|
): Promise<void> {
|
|
const port = parseInt(process.env.PORT ?? "3001", 10);
|
|
|
|
const app = createMcpExpressApp({ host: "0.0.0.0" });
|
|
app.use(cors());
|
|
|
|
app.all("/mcp", async (req: Request, res: Response) => {
|
|
const server = createServer();
|
|
const transport = new NodeStreamableHTTPServerTransport({
|
|
sessionIdGenerator: undefined,
|
|
});
|
|
|
|
res.on("close", () => {
|
|
transport.close().catch(() => {});
|
|
server.close().catch(() => {});
|
|
});
|
|
|
|
try {
|
|
await server.connect(transport);
|
|
await transport.handleRequest(req, res, req.body);
|
|
} catch (error) {
|
|
console.error("MCP error:", error);
|
|
if (!res.headersSent) {
|
|
res.status(500).json({
|
|
jsonrpc: "2.0",
|
|
error: { code: -32603, message: "Internal server error" },
|
|
id: null,
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
const httpServer = app.listen(port, (err) => {
|
|
if (err) {
|
|
console.error("Failed to start server:", err);
|
|
process.exit(1);
|
|
}
|
|
console.log(`MCP server listening on http://localhost:${port}/mcp`);
|
|
});
|
|
|
|
const shutdown = () => {
|
|
console.log("\nShutting down...");
|
|
httpServer.close(() => process.exit(0));
|
|
};
|
|
|
|
process.on("SIGINT", shutdown);
|
|
process.on("SIGTERM", shutdown);
|
|
}
|
|
|
|
/**
|
|
* Starts an MCP server with stdio transport.
|
|
*
|
|
* @param createServer - Factory function that creates a new McpServer instance.
|
|
*/
|
|
export async function startStdioServer(
|
|
createServer: () => McpServer,
|
|
): Promise<void> {
|
|
await createServer().connect(new StdioServerTransport());
|
|
}
|
|
|
|
async function main() {
|
|
if (process.argv.includes("--stdio")) {
|
|
await startStdioServer(createServer);
|
|
} else {
|
|
await startStreamableHTTPServer(createServer);
|
|
}
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|