index.ts•2.09 kB
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"
import { z } from "zod"
// Configuration schema with detailed documentation
export const configSchema = z.object({
debug: z.boolean()
.default(false)
.describe("Enable debug logging for troubleshooting and development. When enabled, tool outputs will include DEBUG: prefix."),
})
export default function createServer({
config,
}: {
config?: z.infer<typeof configSchema> // your server configuration (optional)
}) {
// Parse and validate config with defaults
const validatedConfig = configSchema.parse(config || {})
const server = new McpServer({
name: "Say Hello",
version: "1.0.0",
icon: "icon.svg",
})
// Add a tool
server.registerTool(
"hello",
{
title: "Hello Tool",
description: "Say hello to someone",
inputSchema: { name: z.string().describe("Name to greet") },
annotations: {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: true
}
},
async ({ name }) => ({
content: [
{
type: "text",
text: validatedConfig.debug ? `DEBUG: Hello ${name}` : `Hello, ${name}!` // use validated config
}
],
})
)
// Add a resource
server.registerResource(
"hello-world-history",
"history://hello-world",
{
title: "Hello World History",
description: "The origin story of the famous 'Hello, World' program",
},
async uri => ({
contents: [
{
uri: uri.href,
text: '"Hello, World" first appeared in a 1972 Bell Labs memo by Brian Kernighan and later became the iconic first program for beginners in countless languages.',
mimeType: "text/plain",
},
],
}),
)
// Add a prompt
server.registerPrompt(
"greet",
{
title: "Hello Prompt",
description: "Say hello to someone",
argsSchema: {
name: z.string().describe("Name of the person to greet"),
},
},
async ({ name }) => {
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `Say hello to ${name}`,
},
},
],
}
},
)
return server.server
}