Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions examples/mcp.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { z } from "zod";
import { H3, serve, defineMcpHandler, defineMcpTool, defineMcpResource, defineMcpPrompt } from "h3";

export const app = new H3();

// --- Define MCP tools ---

const echoTool = defineMcpTool({
name: "echo",
description: "Echo back a message",
inputSchema: { message: z.string().describe("The message to echo") },
handler: async ({ message }) => ({
content: [{ type: "text", text: message }],
}),
});

const calculatorTool = defineMcpTool({
name: "calculator",
description: "Perform basic math operations",
inputSchema: {
operation: z.enum(["add", "subtract", "multiply", "divide"]),
a: z.number().describe("First number"),
b: z.number().describe("Second number"),
},
handler: async ({ operation, a, b }) => {
let result;
switch (operation) {
case "add":
result = a + b;
break;
case "subtract":
result = a - b;
break;
case "multiply":
result = a * b;
break;
case "divide":
result = b !== 0 ? a / b : "Error: Division by zero";
break;
}
return {
content: [{ type: "text", text: JSON.stringify({ operation, a, b, result }, null, 2) }],
};
},
});

// --- Define MCP resources ---

const aboutResource = defineMcpResource({
name: "about",
uri: "file:///about",
description: "Information about this MCP server",
handler: async (uri) => ({
contents: [
{
uri: uri.toString(),
text: "This is an example MCP server built with h3.",
},
],
}),
});

// --- Define MCP prompts ---

const summarizePrompt = defineMcpPrompt({
name: "summarize",
description: "Generate a prompt to summarize text",
argsSchema: {
text: z.string().describe("The text to summarize"),
},
handler: async ({ text }) => ({
messages: [
{
role: "user",
content: {
type: "text",
text: `Please summarize the following text:\n\n${text}`,
},
},
],
}),
});

// --- Create the MCP handler ---

app.all(
"/mcp",
defineMcpHandler({
name: "h3-mcp-example",
version: "1.0.0",
tools: [echoTool, calculatorTool],
resources: [aboutResource],
prompts: [summarizePrompt],
}),
);

// --- Landing page ---

app.get("/", () => "MCP server running at /mcp");

serve(app);
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"devDependencies": {
"@happy-dom/global-registrator": "^20.5.0",
"@mitata/counters": "^0.0.8",
"@modelcontextprotocol/sdk": "^1.26.0",
"@types/connect": "^3.4.38",
"@types/express": "^5.0.6",
"@types/node": "^25.2.1",
Expand Down Expand Up @@ -92,11 +93,15 @@
"zod": "^4.3.6"
},
"peerDependencies": {
"@modelcontextprotocol/sdk": "^1.25.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to get rid of dep! #1180 is start i think /cc @sandros94

"crossws": "^0.4.1"
},
"peerDependenciesMeta": {
"crossws": {
"optional": true
},
"@modelcontextprotocol/sdk": {
"optional": true
}
},
"resolutions": {
Expand Down
Loading
Loading