Add optional stdio transport to marketing example servers
When launched with --stdio flag, servers use StdioServerTransport instead of starting an HTTP server. This enables direct integration with Claude Desktop and other MCP clients that use stdio transport. Usage: bun examples/budget-allocator-server/server.ts --stdio 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
ba1a27e465
commit
e516b28d2e
5 changed files with 238 additions and 183 deletions
|
|
@ -5,6 +5,7 @@
|
||||||
* and industry benchmarks by company stage.
|
* and industry benchmarks by company stage.
|
||||||
*/
|
*/
|
||||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
||||||
import type {
|
import type {
|
||||||
CallToolResult,
|
CallToolResult,
|
||||||
|
|
@ -292,50 +293,60 @@ server.registerResource(
|
||||||
);
|
);
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Express Server
|
// Server Startup
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
const app = express();
|
async function main() {
|
||||||
app.use(cors());
|
if (process.argv.includes("--stdio")) {
|
||||||
app.use(express.json());
|
const transport = new StdioServerTransport();
|
||||||
|
|
||||||
app.post("/mcp", async (req: Request, res: Response) => {
|
|
||||||
try {
|
|
||||||
const transport = new StreamableHTTPServerTransport({
|
|
||||||
sessionIdGenerator: undefined,
|
|
||||||
enableJsonResponse: true,
|
|
||||||
});
|
|
||||||
res.on("close", () => {
|
|
||||||
transport.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
await server.connect(transport);
|
await server.connect(transport);
|
||||||
await transport.handleRequest(req, res, req.body);
|
console.error("Budget Allocator Server running in stdio mode");
|
||||||
} catch (error) {
|
} else {
|
||||||
console.error("Error handling MCP request:", error);
|
const app = express();
|
||||||
if (!res.headersSent) {
|
app.use(cors());
|
||||||
res.status(500).json({
|
app.use(express.json());
|
||||||
jsonrpc: "2.0",
|
|
||||||
error: { code: -32603, message: "Internal server error" },
|
app.post("/mcp", async (req: Request, res: Response) => {
|
||||||
id: null,
|
try {
|
||||||
|
const transport = new StreamableHTTPServerTransport({
|
||||||
|
sessionIdGenerator: undefined,
|
||||||
|
enableJsonResponse: true,
|
||||||
|
});
|
||||||
|
res.on("close", () => {
|
||||||
|
transport.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
await server.connect(transport);
|
||||||
|
await transport.handleRequest(req, res, req.body);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error handling MCP request:", 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, () => {
|
||||||
|
console.log(
|
||||||
|
`Budget Allocator Server listening on http://localhost:${PORT}/mcp`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
function shutdown() {
|
||||||
|
console.log("\nShutting down...");
|
||||||
|
httpServer.close(() => {
|
||||||
|
console.log("Server closed");
|
||||||
|
process.exit(0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
process.on("SIGINT", shutdown);
|
||||||
|
process.on("SIGTERM", shutdown);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
const httpServer = app.listen(PORT, () => {
|
|
||||||
console.log(
|
|
||||||
`Budget Allocator Server listening on http://localhost:${PORT}/mcp`,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
function shutdown() {
|
|
||||||
console.log("\nShutting down...");
|
|
||||||
httpServer.close(() => {
|
|
||||||
console.log("Server closed");
|
|
||||||
process.exit(0);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
process.on("SIGINT", shutdown);
|
main().catch(console.error);
|
||||||
process.on("SIGTERM", shutdown);
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
||||||
import type { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
|
import type { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
|
|
@ -204,46 +205,56 @@ const server = new McpServer({
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const app = express();
|
async function main() {
|
||||||
app.use(cors());
|
if (process.argv.includes("--stdio")) {
|
||||||
app.use(express.json());
|
const transport = new StdioServerTransport();
|
||||||
|
|
||||||
app.post("/mcp", async (req: Request, res: Response) => {
|
|
||||||
try {
|
|
||||||
const transport = new StreamableHTTPServerTransport({
|
|
||||||
sessionIdGenerator: undefined,
|
|
||||||
enableJsonResponse: true,
|
|
||||||
});
|
|
||||||
res.on("close", () => {
|
|
||||||
transport.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
await server.connect(transport);
|
await server.connect(transport);
|
||||||
|
console.error("Cohort Heatmap Server running in stdio mode");
|
||||||
|
} else {
|
||||||
|
const app = express();
|
||||||
|
app.use(cors());
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
await transport.handleRequest(req, res, req.body);
|
app.post("/mcp", async (req: Request, res: Response) => {
|
||||||
} catch (error) {
|
try {
|
||||||
console.error("Error handling MCP request:", error);
|
const transport = new StreamableHTTPServerTransport({
|
||||||
if (!res.headersSent) {
|
sessionIdGenerator: undefined,
|
||||||
res.status(500).json({
|
enableJsonResponse: true,
|
||||||
jsonrpc: "2.0",
|
});
|
||||||
error: { code: -32603, message: "Internal server error" },
|
res.on("close", () => {
|
||||||
id: null,
|
transport.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
await server.connect(transport);
|
||||||
|
|
||||||
|
await transport.handleRequest(req, res, req.body);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error handling MCP request:", 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, () => {
|
||||||
|
console.log(`Cohort Heatmap Server listening on http://localhost:${PORT}/mcp`);
|
||||||
|
});
|
||||||
|
|
||||||
|
function shutdown() {
|
||||||
|
console.log("\nShutting down...");
|
||||||
|
httpServer.close(() => {
|
||||||
|
console.log("Server closed");
|
||||||
|
process.exit(0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
process.on("SIGINT", shutdown);
|
||||||
|
process.on("SIGTERM", shutdown);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
const httpServer = app.listen(PORT, () => {
|
|
||||||
console.log(`Server listening on http://localhost:${PORT}/mcp`);
|
|
||||||
});
|
|
||||||
|
|
||||||
function shutdown() {
|
|
||||||
console.log("\nShutting down...");
|
|
||||||
httpServer.close(() => {
|
|
||||||
console.log("Server closed");
|
|
||||||
process.exit(0);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
process.on("SIGINT", shutdown);
|
main().catch(console.error);
|
||||||
process.on("SIGTERM", shutdown);
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
||||||
import type {
|
import type {
|
||||||
CallToolResult,
|
CallToolResult,
|
||||||
|
|
@ -103,48 +104,58 @@ const server = new McpServer({
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const app = express();
|
async function main() {
|
||||||
app.use(cors());
|
if (process.argv.includes("--stdio")) {
|
||||||
app.use(express.json());
|
const transport = new StdioServerTransport();
|
||||||
|
|
||||||
app.post("/mcp", async (req: Request, res: Response) => {
|
|
||||||
try {
|
|
||||||
const transport = new StreamableHTTPServerTransport({
|
|
||||||
sessionIdGenerator: undefined,
|
|
||||||
enableJsonResponse: true,
|
|
||||||
});
|
|
||||||
res.on("close", () => {
|
|
||||||
transport.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
await server.connect(transport);
|
await server.connect(transport);
|
||||||
|
console.error("Customer Segmentation Server running in stdio mode");
|
||||||
|
} else {
|
||||||
|
const app = express();
|
||||||
|
app.use(cors());
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
await transport.handleRequest(req, res, req.body);
|
app.post("/mcp", async (req: Request, res: Response) => {
|
||||||
} catch (error) {
|
try {
|
||||||
console.error("Error handling MCP request:", error);
|
const transport = new StreamableHTTPServerTransport({
|
||||||
if (!res.headersSent) {
|
sessionIdGenerator: undefined,
|
||||||
res.status(500).json({
|
enableJsonResponse: true,
|
||||||
jsonrpc: "2.0",
|
});
|
||||||
error: { code: -32603, message: "Internal server error" },
|
res.on("close", () => {
|
||||||
id: null,
|
transport.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
await server.connect(transport);
|
||||||
|
|
||||||
|
await transport.handleRequest(req, res, req.body);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error handling MCP request:", 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, () => {
|
||||||
|
console.log(
|
||||||
|
`Customer Segmentation Server listening on http://localhost:${PORT}/mcp`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
function shutdown() {
|
||||||
|
console.log("\nShutting down...");
|
||||||
|
httpServer.close(() => {
|
||||||
|
console.log("Server closed");
|
||||||
|
process.exit(0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
process.on("SIGINT", shutdown);
|
||||||
|
process.on("SIGTERM", shutdown);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
const httpServer = app.listen(PORT, () => {
|
|
||||||
console.log(
|
|
||||||
`Customer Segmentation Server listening on http://localhost:${PORT}/mcp`,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
function shutdown() {
|
|
||||||
console.log("\nShutting down...");
|
|
||||||
httpServer.close(() => {
|
|
||||||
console.log("Server closed");
|
|
||||||
process.exit(0);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
process.on("SIGINT", shutdown);
|
main().catch(console.error);
|
||||||
process.on("SIGTERM", shutdown);
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
||||||
import type {
|
import type {
|
||||||
CallToolResult,
|
CallToolResult,
|
||||||
|
|
@ -308,50 +309,60 @@ const server = new McpServer({
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// Express Server
|
// Server Startup
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
const app = express();
|
async function main() {
|
||||||
app.use(cors());
|
if (process.argv.includes("--stdio")) {
|
||||||
app.use(express.json());
|
const transport = new StdioServerTransport();
|
||||||
|
|
||||||
app.post("/mcp", async (req: Request, res: Response) => {
|
|
||||||
try {
|
|
||||||
const transport = new StreamableHTTPServerTransport({
|
|
||||||
sessionIdGenerator: undefined,
|
|
||||||
enableJsonResponse: true,
|
|
||||||
});
|
|
||||||
res.on("close", () => {
|
|
||||||
transport.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
await server.connect(transport);
|
await server.connect(transport);
|
||||||
await transport.handleRequest(req, res, req.body);
|
console.error("SaaS Scenario Modeler Server running in stdio mode");
|
||||||
} catch (error) {
|
} else {
|
||||||
console.error("Error handling MCP request:", error);
|
const app = express();
|
||||||
if (!res.headersSent) {
|
app.use(cors());
|
||||||
res.status(500).json({
|
app.use(express.json());
|
||||||
jsonrpc: "2.0",
|
|
||||||
error: { code: -32603, message: "Internal server error" },
|
app.post("/mcp", async (req: Request, res: Response) => {
|
||||||
id: null,
|
try {
|
||||||
|
const transport = new StreamableHTTPServerTransport({
|
||||||
|
sessionIdGenerator: undefined,
|
||||||
|
enableJsonResponse: true,
|
||||||
|
});
|
||||||
|
res.on("close", () => {
|
||||||
|
transport.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
await server.connect(transport);
|
||||||
|
await transport.handleRequest(req, res, req.body);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error handling MCP request:", 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, () => {
|
||||||
|
console.log(
|
||||||
|
`SaaS Scenario Modeler Server listening on http://localhost:${PORT}/mcp`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
function shutdown() {
|
||||||
|
console.log("\nShutting down...");
|
||||||
|
httpServer.close(() => {
|
||||||
|
console.log("Server closed");
|
||||||
|
process.exit(0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
process.on("SIGINT", shutdown);
|
||||||
|
process.on("SIGTERM", shutdown);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
const httpServer = app.listen(PORT, () => {
|
|
||||||
console.log(
|
|
||||||
`SaaS Scenario Modeler Server listening on http://localhost:${PORT}/mcp`,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
function shutdown() {
|
|
||||||
console.log("\nShutting down...");
|
|
||||||
httpServer.close(() => {
|
|
||||||
console.log("Server closed");
|
|
||||||
process.exit(0);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
process.on("SIGINT", shutdown);
|
main().catch(console.error);
|
||||||
process.on("SIGTERM", shutdown);
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||||
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
||||||
import type {
|
import type {
|
||||||
CallToolResult,
|
CallToolResult,
|
||||||
|
|
@ -175,48 +176,58 @@ const server = new McpServer({
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const app = express();
|
async function main() {
|
||||||
app.use(cors());
|
if (process.argv.includes("--stdio")) {
|
||||||
app.use(express.json());
|
const transport = new StdioServerTransport();
|
||||||
|
|
||||||
app.post("/mcp", async (req: Request, res: Response) => {
|
|
||||||
try {
|
|
||||||
const transport = new StreamableHTTPServerTransport({
|
|
||||||
sessionIdGenerator: undefined,
|
|
||||||
enableJsonResponse: true,
|
|
||||||
});
|
|
||||||
res.on("close", () => {
|
|
||||||
transport.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
await server.connect(transport);
|
await server.connect(transport);
|
||||||
|
console.error("System Monitor Server running in stdio mode");
|
||||||
|
} else {
|
||||||
|
const app = express();
|
||||||
|
app.use(cors());
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
await transport.handleRequest(req, res, req.body);
|
app.post("/mcp", async (req: Request, res: Response) => {
|
||||||
} catch (error) {
|
try {
|
||||||
console.error("Error handling MCP request:", error);
|
const transport = new StreamableHTTPServerTransport({
|
||||||
if (!res.headersSent) {
|
sessionIdGenerator: undefined,
|
||||||
res.status(500).json({
|
enableJsonResponse: true,
|
||||||
jsonrpc: "2.0",
|
});
|
||||||
error: { code: -32603, message: "Internal server error" },
|
res.on("close", () => {
|
||||||
id: null,
|
transport.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
await server.connect(transport);
|
||||||
|
|
||||||
|
await transport.handleRequest(req, res, req.body);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error handling MCP request:", 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, () => {
|
||||||
|
console.log(
|
||||||
|
`System Monitor Server listening on http://localhost:${PORT}/mcp`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
function shutdown() {
|
||||||
|
console.log("\nShutting down...");
|
||||||
|
httpServer.close(() => {
|
||||||
|
console.log("Server closed");
|
||||||
|
process.exit(0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
process.on("SIGINT", shutdown);
|
||||||
|
process.on("SIGTERM", shutdown);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
const httpServer = app.listen(PORT, () => {
|
|
||||||
console.log(
|
|
||||||
`System Monitor Server listening on http://localhost:${PORT}/mcp`,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
function shutdown() {
|
|
||||||
console.log("\nShutting down...");
|
|
||||||
httpServer.close(() => {
|
|
||||||
console.log("Server closed");
|
|
||||||
process.exit(0);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
process.on("SIGINT", shutdown);
|
main().catch(console.error);
|
||||||
process.on("SIGTERM", shutdown);
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue