#!/usr/bin/env node
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { exec } from "child_process";
import { promisify } from "util";
const execAsync = promisify(exec);
// Create server instance
const server = new McpServer({
name: "my-first-mcp",
version: "1.0.0",
});
// Register another tool - get current time
server.registerTool(
"get_time",
{
description: "Gets the current date and time",
},
async () => {
return {
content: [{ type: "text", text: `Current time: ${new Date().toLocaleString()}` }],
};
}
);
// Register new tool - execute command
server.registerTool(
"execute_command",
{
description: "Executes a command in the terminal and returns the output",
inputSchema: z.object({
command: z.string().describe("The command to execute"),
}),
},
async ({ command }) => {
try {
const { stdout, stderr } = await execAsync(command);
return {
content: [
{ type: "text" as const, text: `Output:\n${stdout}` },
...(stderr ? [{ type: "text" as const, text: `Error Output:\n${stderr}` }] : []),
],
};
} catch (error: any) {
return {
content: [{ type: "text" as const, text: `Error executing command: ${error.message}` }],
isError: true,
};
}
}
);
// Start the server
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("MCP Server running on stdio");
}
main().catch(console.error);