Model Context Protocol Model Context Protocol Model Context Protocol · Lab 2/7 Lab 2/7 Lab 2/7 · 25 phút · 25 min · 25 នាទី

02

Thêm tool tùy chỉnh Adding Custom Tools បន្ថែម tool ផ្ទាល់ខ្លួន

Tạo MCP tool tùy chỉnh có schema tham số. Viết tool sinh số ngẫu nhiên. Create custom MCP tools with defined parameter schemas. Implement a random number generator tool. បង្កើត MCP tool ផ្ទាល់ខ្លួនមាន schema ប៉ារ៉ាម៉ែត្រ។ សរសេរ tool បង្កើតលេខចៃដន្យ។

Nội dung bước (lệnh, code) giữ nguyên tiếng Anh từ nguồn chính thức. Step body (commands, code) stays in English from the official source. ខ្លឹមសារជំហាន (ពាក្យបញ្ជា និង code) រក្សាភាសាអង់គ្លេសពីប្រភពផ្លូវការ។ labs.cloudflare.dev ↗

Cần trước Prerequisites តម្រូវការជាមុន

  • Đã xong bước 1 Completed Step 1 បានបញ្ចប់ជំហាន 1
  • MCP server đang chạy MCP server running MCP server កំពុងរត់
  • Đã nối MCP Inspector MCP Inspector connected បានភ្ជាប់ MCP Inspector

Bạn sẽ làm được Learning objectives គោលបំណងសិក្សា

  • Hiểu cách định nghĩa schema tool MCP Understand MCP tool schema definition យល់វិធីកំណត់ schema tool MCP
  • Viết tool tùy chỉnh có validate input Implement custom tools with input validation សរសេរ tool ផ្ទាល់ខ្លួនមាន validate input
  • Test tool tùy chỉnh bằng MCP Inspector Test custom tools using the MCP Inspector Test tool ផ្ទាល់ខ្លួនដោយ MCP Inspector
  • Xử lý lỗi khi chạy tool mà không sập Handle tool execution errors gracefully ដោះស្រាយ error ពេលរត់ tool ដោយមិនដួល

Bước 1: Thêm tool sinh số ngẫu nhiên Step 1: Add a Random Number Generator Tool ជំហាន 1: បន្ថែម tool បង្កើតលេខចៃដន្យ

Chúng ta đang xây What we're building អ្វីដែលយើងកំពុងសង់
A custom random number generator tool with Zod parameter validation.
Vì sao quan trọng Why this matters ហេតុអ្វីសំខាន់
Custom tools extend what AI assistants can do, and every MCP tool follows the same name, schema, handler pattern.

Add the following tool to your src/index.ts inside the init() method, after the existing tools:

Add the following tool to your src/index.ts inside the init() method, after the existing tools:

Add the following tool to your src/index.ts inside the init() method, after the existing tools:

typescript
// inside of async init() { ... }
this.server.tool(
  "generate_random_number",
  "Generates a random number between two numbers",
  { min: z.number(), max: z.number() },
  async ({ min, max }) => ({
    content: [
      {
        type: "text",
        text: String(Math.floor(Math.random() * (max - min + 1)) + min),
      },
    ],
  }),
);

Click the refresh button next to “Available Tools” in the MCP Inspector, and you should see the new “generate_random_number” tool. Try calling it!

Click the refresh button next to “Available Tools” in the MCP Inspector, and you should see the new “generate_random_number” tool. Try calling it!

Click the refresh button next to “Available Tools” in the MCP Inspector, and you should see the new “generate_random_number” tool. Try calling it!

Bước 2: Test tool mới Step 2: Test Your New Tool ជំហាន 2: Test tool ថ្មី

Save the file. Your dev server picks up changes automatically. Head back to the MCP Inspector, click the refresh button next to “Available Tools” to reload, and you should see “generate_random_number” in the list. Test it with {"min": 1, "max": 100}.

Save the file. Your dev server picks up changes automatically. Head back to the MCP Inspector, click the refresh button next to “Available Tools” to reload, and you should see “generate_random_number” in the list. Test it with {"min": 1, "max": 100}.

Save the file. Your dev server picks up changes automatically. Head back to the MCP Inspector, click the refresh button next to “Available Tools” to reload, and you should see “generate_random_number” in the list. Test it with {"min": 1, "max": 100}.

  • Tool not appearing: Make sure you saved the file and the dev server reloaded. Check the terminal for errors.
  • Invalid parameters: Ensure you’re using min and max (not a and b).
  • Inspector disconnected: Reconnect using the same steps from Step 1 - Transport Type: “Streamable HTTP”, URL: http://localhost:8787/mcp.