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:
// 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.