Skip to content

Instantly share code, notes, and snippets.

@mizchi
Last active December 28, 2024 12:41
Show Gist options
  • Save mizchi/d9219a929abd9f070be8439a8317de51 to your computer and use it in GitHub Desktop.
Save mizchi/d9219a929abd9f070be8439a8317de51 to your computer and use it in GitHub Desktop.
Simple MPC Server on Deno (Windows)
{
"mcpServers": {
"mzdev": {
"command": "path\\to\\deno.exe",
"args": ["run", "-A", "path\\to\\main.ts"]
}
}
}
#!/usr/bin/env -S deno run --watch --allow-net --allow-env --allow-run --allow-sys --allow-read --allow-write
// Simple implementation on deno server
import { Server } from "npm:@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "npm:@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListResourcesRequestSchema,
ListToolsRequestSchema,
CallToolResult,
Tool,
CallToolRequest,
} from "npm:@modelcontextprotocol/sdk/types.js";
const TOOLS: Tool[] = [
{
name: "getStringLength",
description: "Get the length of a string",
inputSchema: {
type: "object",
properties: {
input: { type: "string" },
},
required: ["input"],
},
},
];
async function handleToolCall(
name: string,
args: Record<string, unknown>
): Promise<{ toolResult: CallToolResult }> {
switch (name) {
case "getStringLength": {
const input = args.input as string;
if (typeof input !== "string") {
return {
toolResult: {
content: [
{
type: "text",
text: `Expected input to be a string, got ${typeof input}`,
},
],
isError: true,
},
};
} else {
return {
toolResult: {
content: [
{
type: "text",
text: `${Array.from(input).length}`,
},
],
isError: false,
},
};
}
}
default: {
return {
toolResult: {
content: [
{
type: "text",
text: `Unknown tool: ${name}`,
},
],
isError: true,
},
};
}
}
}
const server = new Server(
{
name: "mzdev",
version: "0.1.0",
},
{
capabilities: {
resources: {},
tools: {},
},
}
);
server.setRequestHandler(ListResourcesRequestSchema, () => ({
resources: [],
// resources: [
// {
// uri: "console://logs",
// mimeType: "text/plain",
// name: "Browser console logs",
// },
// ],
}));
server.setRequestHandler(ListToolsRequestSchema, () => ({ tools: TOOLS }));
server.setRequestHandler(CallToolRequestSchema, (request: CallToolRequest) =>
handleToolCall(request.params.name, request.params.arguments ?? {})
);
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("MCP server running on stdio");
@mizchi
Copy link
Author

mizchi commented Dec 27, 2024

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment