npm init -y
npm install @modelcontextprotocol/sdk zod@3
npm install -D @types/node typescript tsx
mkdir src
touch src/index.tspackage.json changes:
{
"type": "module",
"bin": {
"weather": "./build/index.js"
},
"scripts": {
"build": "tsc && chmod 755 build/index.js"
},
"files": ["build"]
}tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "./build",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}src/index.ts:
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
/*
Never use console.log!!! Use console.error instead.
*/
const server = new McpServer({
name: "example-mcp-server",
version: "1.0.0",
});
server.registerTool(
"say_hello",
{
description: "Say hello to a person",
inputSchema: {
name: z.string().describe("The name of the person"),
},
},
async ({ name }) => {
return {
content: [
{
type: "text",
text: `Hello, ${name}!`,
},
],
};
}
);
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Example MCP Server running on stdio");
}
main().catch((error) => {
console.error("Fatal error in main():", error);
process.exit(1);
});npx @modelcontextprotocol/inspector@latest{
"mcpServers": {
"example_server": {
"command": "node",
"args": ["/ABSOLUTE/PATH/TO/FOLDER/build/index.js"]
}
}
}