|
| 1 | +# @modelfetch/deno |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/@modelfetch/deno) |
| 4 | +[](https://opensource.org/licenses/MIT) |
| 5 | +[](https://github.com/phuctm97/modelfetch) |
| 6 | + |
| 7 | +Deno runtime adapter for building MCP (Model Context Protocol) servers with ModelFetch. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +deno add jsr:@modelfetch/deno |
| 13 | +``` |
| 14 | + |
| 15 | +## Quick Start |
| 16 | + |
| 17 | +```typescript |
| 18 | +// server.ts |
| 19 | +import { McpServer } from "npm:@modelcontextprotocol/sdk@^0.7.0/server/mcp.js"; |
| 20 | +import { z } from "npm:zod@^3.24.1"; |
| 21 | + |
| 22 | +const server = new McpServer({ |
| 23 | + title: "My Deno MCP Server", |
| 24 | + name: "my-deno-server", |
| 25 | + version: "1.0.0", |
| 26 | +}); |
| 27 | + |
| 28 | +server.tool( |
| 29 | + "roll_dice", |
| 30 | + "Rolls an N-sided dice", |
| 31 | + { sides: z.number().int().min(2) }, |
| 32 | + ({ sides }) => ({ |
| 33 | + content: [ |
| 34 | + { |
| 35 | + type: "text", |
| 36 | + text: `🎲 You rolled a ${1 + Math.floor(Math.random() * sides)}!`, |
| 37 | + }, |
| 38 | + ], |
| 39 | + }), |
| 40 | +); |
| 41 | + |
| 42 | +export default server; |
| 43 | +``` |
| 44 | + |
| 45 | +```typescript |
| 46 | +// index.ts |
| 47 | +import handle, { getEndpoint } from "jsr:@modelfetch/deno"; |
| 48 | +import server from "./server.ts"; |
| 49 | + |
| 50 | +// Start the server |
| 51 | +const httpServer = handle(server); |
| 52 | +httpServer.finished.then(() => { |
| 53 | + console.log("Server stopped"); |
| 54 | +}); |
| 55 | + |
| 56 | +// Get the server address |
| 57 | +const address = httpServer.addr; |
| 58 | +if (address) { |
| 59 | + console.log(`Server running at: ${getEndpoint(address)}`); |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## API Reference |
| 64 | + |
| 65 | +### `handle(server, options?)` |
| 66 | + |
| 67 | +Starts the MCP server with Deno-specific optimizations and returns a `Deno.HttpServer` instance. |
| 68 | + |
| 69 | +- **server**: The MCP server instance from `@modelcontextprotocol/sdk` |
| 70 | +- **options**: Optional `Deno.ServeOptions` for configuring the HTTP server |
| 71 | + |
| 72 | +Returns: `Deno.HttpServer` |
| 73 | + |
| 74 | +```typescript |
| 75 | +const httpServer = handle(server, { |
| 76 | + port: 3000, |
| 77 | + hostname: "localhost", |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +### `getEndpoint(address)` |
| 82 | + |
| 83 | +Gets the MCP server endpoint URL for connecting clients. |
| 84 | + |
| 85 | +- **address**: A `Deno.Addr` object from the HTTP server |
| 86 | + |
| 87 | +Returns: `string` - The complete endpoint URL |
| 88 | + |
| 89 | +```typescript |
| 90 | +const endpoint = getEndpoint(httpServer.addr); // "http://localhost:3000/mcp" |
| 91 | +``` |
| 92 | + |
| 93 | +## Running Your Server |
| 94 | + |
| 95 | +```bash |
| 96 | +# Development (with all permissions) |
| 97 | +deno run -A index.ts |
| 98 | + |
| 99 | +# Production (with specific permissions) |
| 100 | +deno run --allow-net index.ts |
| 101 | + |
| 102 | +# Compiled executable |
| 103 | +deno compile -A index.ts -o mcp-server |
| 104 | +./mcp-server |
| 105 | +``` |
| 106 | + |
| 107 | +## Permissions |
| 108 | + |
| 109 | +Deno requires explicit permissions. For MCP servers, you typically need: |
| 110 | + |
| 111 | +- `--allow-net`: For the HTTP server |
| 112 | +- `--allow-read`: If reading files |
| 113 | +- `--allow-write`: If writing files |
| 114 | +- `--allow-env`: For environment variables |
| 115 | +- `-A`: Allow all (development only) |
| 116 | + |
| 117 | +## Configuration |
| 118 | + |
| 119 | +Optional `deno.json`: |
| 120 | + |
| 121 | +```json |
| 122 | +{ |
| 123 | + "compilerOptions": { |
| 124 | + "strict": true |
| 125 | + }, |
| 126 | + "imports": { |
| 127 | + "@modelfetch/deno": "jsr:@modelfetch/deno", |
| 128 | + "@modelcontextprotocol/sdk": "npm:@modelcontextprotocol/sdk@^0.7.0", |
| 129 | + "zod": "npm:zod@^3.24.1" |
| 130 | + }, |
| 131 | + "tasks": { |
| 132 | + "dev": "deno run -A index.ts", |
| 133 | + "build": "deno compile -A index.ts" |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +## Documentation |
| 139 | + |
| 140 | +For complete documentation, examples, and guides, visit [modelfetch.com/docs/runtimes/deno](https://modelfetch.com/docs/runtimes/deno). |
| 141 | + |
| 142 | +## License |
| 143 | + |
| 144 | +MIT |
0 commit comments