Model Context Protocol Model Context Protocol Model Context Protocol · Lab 1/7 Lab 1/7 Lab 1/7 · 20 phút · 20 min · 20 នាទី

01

Bắt đầu với MCP server Getting Started with MCP Server ចាប់ផ្តើមជាមួយ MCP server

Dựng MCP server trên Cloudflare Workers. Hiểu cấu trúc cơ bản và test tool bằng Inspector. Set up a functional MCP server using Cloudflare Workers. Understand basic MCP server structure and test MCP tools using the inspector interface. រៀបចំ MCP server លើ Cloudflare Workers។ យល់រចនាសម្ព័ន្ធមូលដ្ឋាន និង test tool ដោយ Inspector។

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 តម្រូវការជាមុន

  • Node.js v20+ Node.js v20+ Node.js v20+
  • Tài khoản Cloudflare Cloudflare Account គណនី Cloudflare
  • Terminal / dòng lệnh Terminal/Command Line Terminal / ពាក្យបញ្ជា
  • Biết JavaScript cơ bản Basic JavaScript knowledge ចេះ JavaScript មូលដ្ឋាន

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

  • Dựng MCP server chạy được trên Cloudflare Workers Set up a functional MCP server with Cloudflare Workers រៀបចំ MCP server ដំណើរការលើ Cloudflare Workers
  • Hiểu cấu trúc và kiến trúc cơ bản của MCP server Understand MCP server basic structure and architecture យល់រចនាសម្ព័ន្ធ និងស្ថាបត្យកម្មមូលដ្ឋានរបស់ MCP server
  • Test MCP tool bằng giao diện Inspector chính thức Test MCP tools using the official inspector interface Test MCP tool ដោយចំណុចប្រទាក់ Inspector ផ្លូវការ
  • Xử lý lỗi kết nối và setup thường gặp Troubleshoot common connection and setup issues ដោះស្រាយបញ្ហាភ្ជាប់ និង setup ធម្មតា

Bước 1 Step 1 ជំហាន 1

Tạo MCP server Create Your MCP Server បង្កើត MCP server

Chúng ta đang xây What we're building អ្វីដែលយើងកំពុងសង់
A Cloudflare Workers-based MCP server that AI assistants can connect to and use.
Vì sao quan trọng Why this matters ហេតុអ្វីសំខាន់
Cloudflare Workers provide global deployment with minimal latency, making your MCP tools available worldwide.
bash
# Create new MCP server from template
npm create cloudflare@latest -- my-mcp-server --template=cloudflare/ai/demos/remote-mcp-authless

# Navigate to your project
cd my-mcp-server

# Install dependencies and start development server
npm install
npm run dev
txt
⛅️ wrangler 4.64.0
───────────────────
Your Worker has access to the following bindings:
Binding                     Resource            Mode
env.MCP_OBJECT (MyMCP)      Durable Object      local╭──────────────────────────────────────────────────────────────────────╮
│ [b] open a browser [d] open devtools [c] clear console [x] to exit │
╰──────────────────────────────────────────────────────────────────────╯
⎔ Starting local server…
[wrangler:info] Ready on http://localhost:8787
  • npm not found: Install Node.js v20+ from nodejs.org
  • Template not found: Update npm: npm install -g npm@latest
  • Port 8787 busy: Stop other services or use npm run dev -- --port 8788
  • Build errors: Delete node_modules and run npm install again
  • Inspector won’t connect: Verify your MCP server is running on http://localhost:8787 and the endpoint is /mcp
  • No tools visible: Check server logs for errors. The template should provide basic tools by default.

Bước 2: Test bằng MCP Inspector Step 2: Test with MCP Inspector ជំហាន 2: Test ដោយ MCP Inspector

Chúng ta đang xây What we're building អ្វីដែលយើងកំពុងសង់
A connection between the MCP Inspector and your server to verify functionality and test tools.
Vì sao quan trọng Why this matters ហេតុអ្វីសំខាន់
The inspector is the easiest way to verify your MCP server is working and explore available tools.
bash
# In a new terminal window (keep your server running)
npx @modelcontextprotocol/inspector

Quy trình kết nối Inspector: Inspector Connection Process: ដំណើរការភ្ជាប់ Inspector៖

  1. The inspector starts at http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=[your_token]
  2. Open this URL in your browser (the inspector will do this automatically)
  3. Make sure “Streamable HTTP” is the Transport Type
  4. Enter http://localhost:8787/mcp as the URL
  5. Click “Connect” to establish the connection
  6. Make sure you’re on the “Tools” tab
  7. Click “List Tools”
  8. Click the “add” tool and run it with 2 numbers

You now have a functional MCP server that AI assistants can connect to and use!

You now have a functional MCP server that AI assistants can connect to and use!

You now have a functional MCP server that AI assistants can connect to and use!