File size: 3,377 Bytes
1c2b077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env node
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);