Last active
July 19, 2025 16:48
-
-
Save kwhinnery/8d2bb724ff5272478c64f09fa08fcb2b to your computer and use it in GitHub Desktop.
MCP server and client code. Client code requires `OPENAI_API_KEY` as an environment variable. Server can be deployed as a single file to Deno Deploy: https://dash.deno.com/playground/dmcp-server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import OpenAI from "jsr:@openai/[email protected]"; | |
const client = new OpenAI(); | |
const res = await client.responses.create({ | |
model: "o4-mini", | |
input: "Roll 10d6 + 1", | |
tools: [{ | |
type: "mcp", | |
server_label: "dmcp", | |
server_url: "https://dmcp-server.deno.dev/sse", | |
server_description: "DMCP - MCP for Dungeons and Dragons", | |
allowed_tools: ["roll"], | |
require_approval: "never", | |
}], | |
}); | |
console.log(JSON.stringify(res.output, null, 2)); | |
console.log("\n" + res.output_text); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { FastMCP } from "npm:[email protected]"; | |
import { z } from "npm:[email protected]"; | |
import { roll } from "npm:@half-elf/[email protected]"; | |
const server = new FastMCP({ | |
name: "DMCP - MCP for Dungeons and Dragons", | |
version: "0.1.0", | |
}); | |
const description = ` | |
Given a string of text describing a dice roll in Dungeons and Dragons, | |
provide a random result of the roll. | |
Example input: 2d6 + 1d4 | |
Example output: 14 | |
`; | |
server.addTool({ | |
name: "roll", | |
description, | |
parameters: z.object({ diceRollExpression: z.string() }), | |
execute: async (args) => { | |
return String(roll(args.diceRollExpression)); | |
}, | |
}); | |
server.start({ | |
transportType: "httpStream", | |
httpStream: { port: parseInt(Deno.env.get("PORT") || "8000") }, | |
}); |
Comments are disabled for this gist.