spinny:~/writing $ vim mcp-model-context-protocol-guide.md
1~2**Model Context Protocol (MCP)** เป็นมาตรฐานเปิดที่สร้างโดย Anthropic ซึ่งกำหนดวิธีที่โมเดล AI สื่อสารกับเครื่องมือภายนอก แหล่งข้อมูล และบริการต่างๆ ลองคิดว่ามันเป็น "USB-C ของ AI" - ตัวเชื่อมต่อสากลที่ช่วยให้ AI Agent ใดๆ สามารถพูดคุยกับเครื่องมือใดๆ ผ่านอินเทอร์เฟซมาตรฐาน3~4นับตั้งแต่เปิดตัว MCP มี**ยอดดาวน์โหลด SDK มากกว่า 97 ล้านครั้งต่อเดือน** และได้รับการยอมรับจากผู้ให้บริการ AI รายใหญ่ทุกราย: Anthropic, OpenAI, Google, Microsoft และ Amazon ในคู่มือนี้ เราจะสำรวจทุกสิ่งที่คุณจำเป็นต้องรู้เพื่อสร้างงานด้วย MCP5~6## ทำไม MCP ถึงมีอยู่7~8ก่อน MCP แอปพลิเคชัน AI ทุกตัวต้องสร้างการเชื่อมต่อแบบกำหนดเองสำหรับเครื่องมือแต่ละตัวที่ต้องการใช้ อยากให้ AI อ่านไฟล์? เขียนโค้ดกำหนดเอง สอบถามฐานข้อมูล? โค้ดกำหนดเองเพิ่มเติม โพสต์ไปยัง Slack? การเชื่อมต่ออีกตัว9~10สิ่งนี้สร้าง**ปัญหา N×M**: แอปพลิเคชัน AI จำนวน N ตัว แต่ละตัวต้องการการเชื่อมต่อกำหนดเอง M ตัว ส่งผลให้เกิดความพยายามซ้ำซ้อนและระบบนิเวศที่กระจัดกระจาย11~12```mermaid13graph TD14 subgraph "Before MCP (N×M)"15 A1[AI App 1] --> T1[GitHub Integration]16 A1 --> T2[Slack Integration]17 A1 --> T3[DB Integration]18 A2[AI App 2] --> T4[GitHub Integration]19 A2 --> T5[Slack Integration]20 A2 --> T6[DB Integration]21 end22```23~24MCP แก้ปัญหานี้ด้วย**โปรโตคอลเดียว**ที่ไคลเอนต์ AI ใดๆ สามารถใช้สื่อสารกับเซิร์ฟเวอร์ MCP ใดๆ:25~26```mermaid27graph TD28 subgraph "With MCP (N+M)"29 A1[AI App 1] --> MCP[MCP Protocol]30 A2[AI App 2] --> MCP31 MCP --> S1[GitHub Server]32 MCP --> S2[Slack Server]33 MCP --> S3[DB Server]34 end35```36~37## แนวคิดหลัก38~39MCP มีพื้นฐานสามประการ:40~41### 1. เครื่องมือ (Tools)42เครื่องมือคือฟังก์ชันที่ AI สามารถเรียกใช้ได้ มันแทนการกระทำเช่น "สร้างไฟล์", "สอบถามฐานข้อมูล" หรือ "ส่งข้อความ" เครื่องมือแต่ละตัวมีชื่อ คำอธิบาย และ JSON Schema สำหรับพารามิเตอร์43~44```typescript45{46 name: "create_issue",47 description: "Create a new GitHub issue",48 inputSchema: {49 type: "object",50 properties: {51 title: { type: "string", description: "Issue title" },52 body: { type: "string", description: "Issue body" },53 repo: { type: "string", description: "Repository name" }54 },55 required: ["title", "repo"]56 }57}58```59~60### 2. ทรัพยากร (Resources)61ทรัพยากรคือข้อมูลที่ AI สามารถอ่านได้ มันแทนไฟล์ เรคคอร์ดฐานข้อมูล การตอบกลับ API หรือแหล่งข้อมูลอื่นๆ ทรัพยากรถูกระบุด้วย URI62~63```typescript64{65 uri: "file:///Users/dev/project/README.md",66 name: "Project README",67 mimeType: "text/markdown"68}69```70~71### 3. พรอมต์ (Prompts)72พรอมต์คือเทมเพลตที่ใช้ซ้ำได้ซึ่งช่วยจัดโครงสร้างการโต้ตอบ สามารถรวมพารามิเตอร์แบบไดนามิกและมีประโยชน์สำหรับการกำหนดมาตรฐานเวิร์กโฟลว์ทั่วไป73~74```typescript75{76 name: "code_review",77 description: "Review code changes for quality and security",78 arguments: [79 { name: "diff", description: "The code diff to review", required: true }80 ]81}82```83~84## สถาปัตยกรรม85~86MCP ใช้สถาปัตยกรรมไคลเอนต์-เซิร์ฟเวอร์:87~88```mermaid89graph LR90 subgraph "Host Application"91 Client[MCP Client]92 end93~94 subgraph "MCP Server"95 Server[Server Process]96 Server --> Tools[Tools]97 Server --> Resources[Resources]98 Server --> Prompts[Prompts]99 end100~101 Client -- "JSON-RPC 2.0\n(stdio or SSE)" --> Server102```103~104- **โฮสต์**: แอปพลิเคชัน AI (Claude Desktop, Cursor, แอปกำหนดเองของคุณ)105- **ไคลเอนต์**: รักษาการเชื่อมต่อ 1:1 กับเซิร์ฟเวอร์ MCP106- **เซิร์ฟเวอร์**: เปิดเผยเครื่องมือ ทรัพยากร และพรอมต์ให้กับไคลเอนต์107- **ทรานสปอร์ต**: การสื่อสารเกิดขึ้นผ่าน JSON-RPC 2.0 บน stdio (ในเครื่อง) หรือ Server-Sent Events (ระยะไกล)108~109## การสร้างเซิร์ฟเวอร์ MCP110~111มาสร้างเซิร์ฟเวอร์ MCP ในทางปฏิบัติที่โต้ตอบกับรายการสิ่งที่ต้องทำที่เก็บในไฟล์ JSON112~113### การตั้งค่า114~115```bash116mkdir mcp-todo-server && cd mcp-todo-server117npm init -y118npm install @modelcontextprotocol/sdk zod119```120~121### การพัฒนาเซิร์ฟเวอร์122~123```typescript124// src/index.ts125import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";126import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";127import { z } from "zod";128import fs from "fs";129~130const TODO_FILE = "./todos.json";131~132function readTodos(): { id: number; text: string; done: boolean }[] {133 if (!fs.existsSync(TODO_FILE)) return [];134 return JSON.parse(fs.readFileSync(TODO_FILE, "utf-8"));135}136~137function writeTodos(todos: { id: number; text: string; done: boolean }[]) {138 fs.writeFileSync(TODO_FILE, JSON.stringify(todos, null, 2));139}140~141const server = new McpServer({142 name: "todo-server",143 version: "1.0.0",144});145~146// Tool: Add a todo147server.tool(148 "add_todo",149 "Add a new todo item",150 { text: z.string().describe("The todo text") },151 async ({ text }) => {152 const todos = readTodos();153 const newTodo = { id: Date.now(), text, done: false };154 todos.push(newTodo);155 writeTodos(todos);156 return { content: [{ type: "text", text: `Added: "${text}"` }] };157 }158);159~160// Tool: List todos161server.tool(162 "list_todos",163 "List all todo items",164 {},165 async () => {166 const todos = readTodos();167 const list = todos168 .map((t) => `${t.done ? "✅" : "⬜"} [${t.id}] ${t.text}`)169 .join("\n");170 return { content: [{ type: "text", text: list || "No todos yet." }] };171 }172);173~174// Tool: Complete a todo175server.tool(176 "complete_todo",177 "Mark a todo as completed",178 { id: z.number().describe("The todo ID to complete") },179 async ({ id }) => {180 const todos = readTodos();181 const todo = todos.find((t) => t.id === id);182 if (!todo) return { content: [{ type: "text", text: "Todo not found." }] };183 todo.done = true;184 writeTodos(todos);185 return { content: [{ type: "text", text: `Completed: "${todo.text}"` }] };186 }187);188~189// Resource: Current todos as a readable resource190server.resource(191 "todos://list",192 "Current todo list",193 async () => ({194 contents: [{195 uri: "todos://list",196 mimeType: "application/json",197 text: JSON.stringify(readTodos(), null, 2),198 }],199 })200);201~202// Start the server203const transport = new StdioServerTransport();204await server.connect(transport);205```206~207### การกำหนดค่า208~209เพื่อใช้เซิร์ฟเวอร์นี้กับ Claude Desktop ให้เพิ่มในการกำหนดค่า:210~211```json212{213 "mcpServers": {214 "todo": {215 "command": "npx",216 "args": ["tsx", "/path/to/mcp-todo-server/src/index.ts"]217 }218 }219}220```221~222## การสร้างไคลเอนต์ MCP223~224คุณยังสามารถสร้างไคลเอนต์กำหนดเองที่เชื่อมต่อกับเซิร์ฟเวอร์ MCP ใดๆ:225~226```typescript227import { Client } from "@modelcontextprotocol/sdk/client/index.js";228import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";229~230const transport = new StdioClientTransport({231 command: "npx",232 args: ["tsx", "./src/index.ts"],233});234~235const client = new Client({ name: "my-client", version: "1.0.0" });236await client.connect(transport);237~238// List available tools239const { tools } = await client.listTools();240console.log("Available tools:", tools.map((t) => t.name));241~242// Call a tool243const result = await client.callTool({244 name: "add_todo",245 arguments: { text: "Write MCP blog post" },246});247console.log(result);248```249~250## เซิร์ฟเวอร์ MCP ยอดนิยม251~252ระบบนิเวศ MCP เติบโตอย่างรวดเร็ว นี่คือเซิร์ฟเวอร์ยอดนิยมบางตัว:253~254| เซิร์ฟเวอร์ | คำอธิบาย | กรณีการใช้งาน |255|-------------|---------|--------------|256| **GitHub** | สร้าง issue, PR, จัดการ repo | เวิร์กโฟลว์การพัฒนา |257| **Slack** | ส่งข้อความ, จัดการช่อง | การสื่อสารทีม |258| **PostgreSQL** | สอบถามและจัดการฐานข้อมูล | การเข้าถึงข้อมูล |259| **Filesystem** | อ่าน เขียน และค้นหาไฟล์ | การพัฒนาในเครื่อง |260| **Puppeteer** | การทำงานอัตโนมัติของเบราว์เซอร์และสแครป | การทดสอบเว็บ |261| **Sentry** | การตรวจสอบข้อผิดพลาดและการดีบัก | การสนับสนุนโปรดักชัน |262| **Supabase** | ฐานข้อมูล, การยืนยันตัวตน, ที่เก็บข้อมูล | การดำเนินงานแบ็กเอนด์ |263~264## MCP เทียบกับ A2A (Agent-to-Agent)265~266ในขณะที่ MCP จัดการการสื่อสาร**ตัวแทน-เครื่องมือ** โปรโตคอล **A2A (Agent-to-Agent)** ของ Google จัดการการสื่อสาร**ตัวแทน-ตัวแทน** ทั้งสองเสริมซึ่งกันและกัน:267~268```mermaid269graph TD270 Agent1[Agent 1] -- "A2A Protocol" --> Agent2[Agent 2]271 Agent1 -- "MCP Protocol" --> Tool1[GitHub MCP Server]272 Agent2 -- "MCP Protocol" --> Tool2[Database MCP Server]273```274~275- **MCP**: AI Agent ใช้เครื่องมืออย่างไร (การรวมแนวตั้ง)276- **A2A**: AI Agent ร่วมมือกันอย่างไร (การรวมแนวนอน)277~278## แนวทางปฏิบัติที่ดีที่สุด279~2801. **ให้เซิร์ฟเวอร์มุ่งเน้น**: หนึ่งเซิร์ฟเวอร์ต่อโดเมน (เซิร์ฟเวอร์ GitHub, เซิร์ฟเวอร์ Slack ฯลฯ) อย่าสร้างเซิร์ฟเวอร์แบบ monolithic2812. **ตรวจสอบอินพุตด้วย Zod**: ตรวจสอบอินพุตเครื่องมือด้วยสคีมาที่เหมาะสมเสมอ2823. **จัดการข้อผิดพลาดอย่างสง่างาม**: ส่งคืนข้อความข้อผิดพลาดที่มีความหมาย ไม่ใช่ stack trace2834. **ใช้ทรัพยากรสำหรับข้อมูลอ่านอย่างเดียว**: หาก AI ต้องการเพียงอ่านข้อมูล ให้เปิดเผยเป็นทรัพยากรแทนเครื่องมือ2845. **เพิ่มคำอธิบายที่เหมาะสม**: คำอธิบายเครื่องมือและพารามิเตอร์ที่ดีช่วยให้ AI เข้าใจว่าจะใช้เครื่องมือแต่ละตัวเมื่อใดและอย่างไร2856. **ทดสอบด้วย MCP Inspector**: ใช้ `npx @modelcontextprotocol/inspector` เพื่อทดสอบเซิร์ฟเวอร์แบบโต้ตอบ286~287## เริ่มต้น288~289```bash290# Create a new MCP server from template291npx @modelcontextprotocol/create-server my-server292~293# Test with the MCP Inspector294npx @modelcontextprotocol/inspector npx tsx ./src/index.ts295```296~297## สรุป298~299MCP กลายเป็นมาตรฐานโดยพฤตินัยสำหรับการใช้เครื่องมือ AI ในปี 2026 ไม่ว่าคุณจะกำลังสร้าง AI Agent แบบกำหนดเอง ขยาย Claude Code หรือสร้างการเชื่อมต่อสำหรับแพลตฟอร์มที่มีอยู่ การเข้าใจ MCP เป็นสิ่งจำเป็น โปรโตคอลนี้เรียบง่ายพอที่จะเรียนรู้ได้ในบ่ายเดียว แต่ทรงพลังพอที่จะสร้างเวิร์กโฟลว์ AI ระดับโปรดักชัน300~301**ขั้นตอนถัดไป:**302- สำรวจ [ข้อกำหนด MCP](https://spec.modelcontextprotocol.io/)303- เรียกดู[เซิร์ฟเวอร์ MCP ที่มีอยู่](https://github.com/modelcontextprotocol/servers) เพื่อแรงบันดาลใจ304- สร้างเซิร์ฟเวอร์แรกด้วย TypeScript หรือ Python SDK305- ทดสอบด้วย Claude Desktop หรือ MCP Inspector306~
NORMAL · mcp-model-context-protocol-guide.md [readonly]306 lines · :q to close