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:
Anton Pidkuiko MacBook 2025-12-08 13:20:07 +00:00
commit e516b28d2e
No known key found for this signature in database
5 changed files with 238 additions and 183 deletions

View file

@ -5,6 +5,7 @@
* and industry benchmarks by company stage.
*/
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 type {
CallToolResult,
@ -292,14 +293,20 @@ server.registerResource(
);
// ---------------------------------------------------------------------------
// Express Server
// Server Startup
// ---------------------------------------------------------------------------
const app = express();
app.use(cors());
app.use(express.json());
async function main() {
if (process.argv.includes("--stdio")) {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Budget Allocator Server running in stdio mode");
} else {
const app = express();
app.use(cors());
app.use(express.json());
app.post("/mcp", async (req: Request, res: Response) => {
app.post("/mcp", async (req: Request, res: Response) => {
try {
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: undefined,
@ -321,21 +328,25 @@ app.post("/mcp", async (req: Request, res: Response) => {
});
}
}
});
});
const httpServer = app.listen(PORT, () => {
const httpServer = app.listen(PORT, () => {
console.log(
`Budget Allocator Server listening on http://localhost:${PORT}/mcp`,
);
});
});
function shutdown() {
function shutdown() {
console.log("\nShutting down...");
httpServer.close(() => {
console.log("Server closed");
process.exit(0);
});
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
}
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
main().catch(console.error);

View file

@ -1,4 +1,5 @@
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 type { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
import cors from "cors";
@ -204,11 +205,17 @@ const server = new McpServer({
);
}
const app = express();
app.use(cors());
app.use(express.json());
async function main() {
if (process.argv.includes("--stdio")) {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Cohort Heatmap Server running in stdio mode");
} else {
const app = express();
app.use(cors());
app.use(express.json());
app.post("/mcp", async (req: Request, res: Response) => {
app.post("/mcp", async (req: Request, res: Response) => {
try {
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: undefined,
@ -231,19 +238,23 @@ app.post("/mcp", async (req: Request, res: Response) => {
});
}
}
});
});
const httpServer = app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}/mcp`);
});
const httpServer = app.listen(PORT, () => {
console.log(`Cohort Heatmap Server listening on http://localhost:${PORT}/mcp`);
});
function shutdown() {
function shutdown() {
console.log("\nShutting down...");
httpServer.close(() => {
console.log("Server closed");
process.exit(0);
});
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
}
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
main().catch(console.error);

View file

@ -1,4 +1,5 @@
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 type {
CallToolResult,
@ -103,11 +104,17 @@ const server = new McpServer({
);
}
const app = express();
app.use(cors());
app.use(express.json());
async function main() {
if (process.argv.includes("--stdio")) {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Customer Segmentation Server running in stdio mode");
} else {
const app = express();
app.use(cors());
app.use(express.json());
app.post("/mcp", async (req: Request, res: Response) => {
app.post("/mcp", async (req: Request, res: Response) => {
try {
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: undefined,
@ -130,21 +137,25 @@ app.post("/mcp", async (req: Request, res: Response) => {
});
}
}
});
});
const httpServer = app.listen(PORT, () => {
const httpServer = app.listen(PORT, () => {
console.log(
`Customer Segmentation Server listening on http://localhost:${PORT}/mcp`,
);
});
});
function shutdown() {
function shutdown() {
console.log("\nShutting down...");
httpServer.close(() => {
console.log("Server closed");
process.exit(0);
});
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
}
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
main().catch(console.error);

View file

@ -1,4 +1,5 @@
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 type {
CallToolResult,
@ -308,14 +309,20 @@ const server = new McpServer({
}
// ============================================================================
// Express Server
// Server Startup
// ============================================================================
const app = express();
app.use(cors());
app.use(express.json());
async function main() {
if (process.argv.includes("--stdio")) {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("SaaS Scenario Modeler Server running in stdio mode");
} else {
const app = express();
app.use(cors());
app.use(express.json());
app.post("/mcp", async (req: Request, res: Response) => {
app.post("/mcp", async (req: Request, res: Response) => {
try {
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: undefined,
@ -337,21 +344,25 @@ app.post("/mcp", async (req: Request, res: Response) => {
});
}
}
});
});
const httpServer = app.listen(PORT, () => {
const httpServer = app.listen(PORT, () => {
console.log(
`SaaS Scenario Modeler Server listening on http://localhost:${PORT}/mcp`,
);
});
});
function shutdown() {
function shutdown() {
console.log("\nShutting down...");
httpServer.close(() => {
console.log("Server closed");
process.exit(0);
});
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
}
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
main().catch(console.error);

View file

@ -1,4 +1,5 @@
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 type {
CallToolResult,
@ -175,11 +176,17 @@ const server = new McpServer({
);
}
const app = express();
app.use(cors());
app.use(express.json());
async function main() {
if (process.argv.includes("--stdio")) {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("System Monitor Server running in stdio mode");
} else {
const app = express();
app.use(cors());
app.use(express.json());
app.post("/mcp", async (req: Request, res: Response) => {
app.post("/mcp", async (req: Request, res: Response) => {
try {
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: undefined,
@ -202,21 +209,25 @@ app.post("/mcp", async (req: Request, res: Response) => {
});
}
}
});
});
const httpServer = app.listen(PORT, () => {
const httpServer = app.listen(PORT, () => {
console.log(
`System Monitor Server listening on http://localhost:${PORT}/mcp`,
);
});
});
function shutdown() {
function shutdown() {
console.log("\nShutting down...");
httpServer.close(() => {
console.log("Server closed");
process.exit(0);
});
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
}
}
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
main().catch(console.error);