Skip to content

Latest commit

 

History

History
86 lines (74 loc) · 1.74 KB

File metadata and controls

86 lines (74 loc) · 1.74 KB

範例

這是一個用於 MCP 伺服器的 JavaScript 範例

以下是計算器部分的樣子:

// Define calculator tools for each operation
server.tool(
  "add",
  {
    a: z.number(),
    b: z.number()
  },
  async ({ a, b }) => ({
    content: [{ type: "text", text: String(a + b) }]
  })
);

server.tool(
  "subtract",
  {
    a: z.number(),
    b: z.number()
  },
  async ({ a, b }) => ({
    content: [{ type: "text", text: String(a - b) }]
  })
);

server.tool(
  "multiply",
  {
    a: z.number(),
    b: z.number()
  },
  async ({ a, b }) => ({
    content: [{ type: "text", text: String(a * b) }]
  })
);

server.tool(
  "divide",
  {
    a: z.number(),
    b: z.number()
  },
  async ({ a, b }) => {
    if (b === 0) {
      return {
        content: [{ type: "text", text: "Error: Cannot divide by zero" }],
        isError: true
      };
    }
    return {
      content: [{ type: "text", text: String(a / b) }]
    };
  }
);

安裝

執行以下命令:

npm install

運行

npm start

免責聲明: 本文檔使用AI翻譯服務Co-op Translator進行翻譯。我們努力確保準確性,但請注意,自動翻譯可能包含錯誤或不準確之處。應以原始語言的文件為權威來源。對於關鍵信息,建議尋求專業人工翻譯。對於因使用此翻譯而產生的任何誤解或誤譯,我們不承擔責任。