|
| 1 | +import { serve } from "@hono/node-server"; |
| 2 | +import { Hono } from "hono"; |
| 3 | +import { MCPServer } from "@zuplo/mcp/server"; |
| 4 | +import { HTTPStreamableTransport } from "@zuplo/mcp/transport/httpstreamable"; |
| 5 | +import { ConsoleLogger } from "@zuplo/mcp/logger"; |
| 6 | +import { ResourceMetadata, ResourceReader, URITemplate } from "@zuplo/mcp/resources"; |
| 7 | + |
| 8 | +const app = new Hono(); |
| 9 | + |
| 10 | +const logger = new ConsoleLogger(); |
| 11 | + |
| 12 | +const server = new MCPServer({ |
| 13 | + name: "Resources Example Server", |
| 14 | + version: "1.0.0", |
| 15 | + logger, |
| 16 | + capabilities: { |
| 17 | + resources: {}, |
| 18 | + }, |
| 19 | +}); |
| 20 | + |
| 21 | +server.addResource( |
| 22 | + "document", |
| 23 | + "file:///example/document.txt", |
| 24 | + { |
| 25 | + title: "Example Document", |
| 26 | + description: "A sample text document", |
| 27 | + mimeType: "text/plain", |
| 28 | + annotations: { |
| 29 | + audience: ["user" as const, "assistant" as const], |
| 30 | + priority: 0.8, |
| 31 | + }, |
| 32 | + }, |
| 33 | + async (uri) => ({ |
| 34 | + contents: [ |
| 35 | + { |
| 36 | + uri, |
| 37 | + mimeType: "text/plain", |
| 38 | + text: "This is a sample document.\n\nIt contains multiple lines of text that demonstrate how resources work in the MCP protocol.", |
| 39 | + }, |
| 40 | + ], |
| 41 | + }) |
| 42 | +); |
| 43 | + |
| 44 | +server.addResource( |
| 45 | + "data", |
| 46 | + "file:///example/data.json", |
| 47 | + { |
| 48 | + title: "Sample Data", |
| 49 | + description: "JSON data file with sample information", |
| 50 | + mimeType: "application/json", |
| 51 | + annotations: { |
| 52 | + audience: ["assistant" as const], |
| 53 | + priority: 0.5, |
| 54 | + }, |
| 55 | + }, |
| 56 | + async (uri) => ({ |
| 57 | + contents: [ |
| 58 | + { |
| 59 | + uri, |
| 60 | + mimeType: "application/json", |
| 61 | + text: JSON.stringify( |
| 62 | + { |
| 63 | + name: "Example Data", |
| 64 | + items: [ |
| 65 | + { id: 1, value: "First item" }, |
| 66 | + { id: 2, value: "Second item" }, |
| 67 | + { id: 3, value: "Third item" }, |
| 68 | + ], |
| 69 | + metadata: { |
| 70 | + created: "2025-01-01", |
| 71 | + version: "1.0", |
| 72 | + }, |
| 73 | + }, |
| 74 | + null, |
| 75 | + 2 |
| 76 | + ), |
| 77 | + }, |
| 78 | + ], |
| 79 | + }) |
| 80 | +); |
| 81 | + |
| 82 | +server.addResource( |
| 83 | + "image", |
| 84 | + "file:///example/image.png", |
| 85 | + { |
| 86 | + title: "Example Image", |
| 87 | + description: "A sample PNG image", |
| 88 | + mimeType: "image/png", |
| 89 | + }, |
| 90 | + async (uri) => ({ |
| 91 | + contents: [ |
| 92 | + { |
| 93 | + uri, |
| 94 | + mimeType: "image/png", |
| 95 | + blob: "aW1hZ2Vwbmc=", |
| 96 | + }, |
| 97 | + ], |
| 98 | + }) |
| 99 | +); |
| 100 | + |
| 101 | +const uriTemp: URITemplate = { template: "file:///example/{filename}" }; |
| 102 | +const resMetadata: ResourceMetadata = { |
| 103 | + title: "📁 Example Files", |
| 104 | + description: "Access any file in the example directory by name", |
| 105 | + annotations: { |
| 106 | + audience: ["user" as const], |
| 107 | + priority: 0.6, |
| 108 | + } |
| 109 | +}; |
| 110 | + |
| 111 | +const resHandler: ResourceReader = async (uri) => { |
| 112 | + const filename = uri.split("/").pop(); |
| 113 | + return { |
| 114 | + contents: [ |
| 115 | + { |
| 116 | + uri, |
| 117 | + mimeType: "text/plain", |
| 118 | + text: `This is a dynamically generated file: ${filename}`, |
| 119 | + }, |
| 120 | + ], |
| 121 | + }; |
| 122 | +}; |
| 123 | + |
| 124 | +server.addResource( |
| 125 | + "files", |
| 126 | + uriTemp, |
| 127 | + resMetadata, |
| 128 | + resHandler |
| 129 | +); |
| 130 | + |
| 131 | +const transport = new HTTPStreamableTransport({ logger }); |
| 132 | +await transport.connect(); |
| 133 | +server.withTransport(transport); |
| 134 | + |
| 135 | +app.post("/mcp", async (c) => { |
| 136 | + try { |
| 137 | + const request = c.req.raw; |
| 138 | + return transport.handleRequest(request); |
| 139 | + } catch (error) { |
| 140 | + logger.error("Error handling MCP request:", error); |
| 141 | + return new Response(JSON.stringify({ error: "Internal server error" }), { |
| 142 | + status: 500, |
| 143 | + headers: { "Content-Type": "application/json" }, |
| 144 | + }); |
| 145 | + } |
| 146 | +}); |
| 147 | + |
| 148 | +logger.info("Resources MCP server starting on port 3000"); |
| 149 | +serve({ |
| 150 | + fetch: app.fetch, |
| 151 | + port: 3000 |
| 152 | +}); |
0 commit comments