Spaces:
Runtime error
Runtime error
| import { Server } from "@modelcontextprotocol/sdk/server/index.js"; | |
| import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; | |
| import { | |
| ErrorCode, | |
| ListResourcesRequestSchema, | |
| ListResourcesResult, | |
| ListResourceTemplatesRequestSchema, | |
| McpError, | |
| ReadResourceRequestSchema, | |
| } from "@modelcontextprotocol/sdk/types.js"; | |
| interface Resource { | |
| name: string; | |
| description: string; | |
| inputSchema: { | |
| type: string; | |
| properties: Record< | |
| string, | |
| { | |
| type: string; | |
| description: string; | |
| } | |
| >; | |
| required?: string[]; | |
| }; | |
| } | |
| class StickerServer { | |
| private server: Server; | |
| constructor() { | |
| this.server = new Server( | |
| { | |
| name: "sticker-server", | |
| version: "1.0.0", | |
| }, | |
| { | |
| capabilities: { | |
| resources: {}, | |
| }, | |
| } | |
| ); | |
| this.setupResourceHandlers(); | |
| this.server.onerror = (error: Error) => console.error("[MCP Error]", error); | |
| process.on("SIGINT", async () => { | |
| await this.server.close(); | |
| process.exit(0); | |
| }); | |
| } | |
| private setupResourceHandlers(): void { | |
| this.server.setRequestHandler( | |
| ListResourceTemplatesRequestSchema, | |
| async () => ({ | |
| resourceTemplates: [ | |
| { | |
| uriTemplate: | |
| "mcp://sticker-server/send_sticker?description={description}", | |
| name: "send sticker", | |
| mimeType: "text", | |
| description: | |
| "请正常回复用户的问题,只有在正常回答用户问题后,根据情境需要,使用此 Resource 读取一个表达特定情绪或描述的贴纸的 URL,并将图片展示到对话内,需要展示图片,而不是 URL", | |
| }, | |
| ], | |
| }) | |
| ); | |
| // Read resource contents | |
| this.server.setRequestHandler( | |
| ReadResourceRequestSchema, | |
| async (request) => { | |
| try { | |
| const uri = request.params.uri; | |
| // mcp://sticker-server/send_sticker?description={description} | |
| const description = new URL(uri).searchParams.get("description"); | |
| const response = await fetch( | |
| "http://127.0.0.1:7860/api/search_stickers", | |
| { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| }, | |
| body: JSON.stringify({ description }), | |
| } | |
| ); | |
| const stickerList = await response.json(); | |
| console.log("uri", uri, "stickerList", stickerList); | |
| const imageUrl = stickerList?.[0]?.image_url || "找不到贴纸呢~"; | |
| await new Promise((resolve) => setTimeout(resolve, 1000)); | |
| return { | |
| contents: [ | |
| { | |
| uri, | |
| type: "text", | |
| text: imageUrl, | |
| }, | |
| ], | |
| }; | |
| } catch (error) { | |
| return { | |
| contents: [ | |
| { | |
| uri: "", | |
| type: "text", | |
| text: JSON.stringify(error), | |
| }, | |
| ], | |
| }; | |
| } | |
| } | |
| ); | |
| } | |
| async run(): Promise<void> { | |
| const transport = new StdioServerTransport(); | |
| await this.server.connect(transport); | |
| console.error("Sticker MCP server running on stdio"); | |
| } | |
| } | |
| const server = new StickerServer(); | |
| server.run().catch(console.error); | |