```html
การพัฒนา AI กำลังพัฒนาอย่างรวดเร็ว และการผสานรวมเครื่องมือภายนอกกับแบบจำลองภาษาเป็นก้าวสำคัญไปข้างหน้า OpenRouter มอบ API แบบครบวงจรเพื่อเข้าถึงแบบจำลองภาษาจำนวนมาก ในขณะที่ MCP Servers (Model Context Protocol Servers) ช่วยให้แบบจำลองเหล่านี้สามารถเรียกใช้เครื่องมือภายนอกและเข้าถึงข้อมูลสดได้ การรวมเข้าด้วยกันจะสร้างระบบอันทรงพลังสำหรับการสร้างแอปพลิเคชัน AI ขั้นสูง
ในโพสต์นี้ ผมจะแนะนำคุณตลอดการผสานรวม MCP Servers กับ OpenRouter คุณจะได้เรียนรู้ฟังก์ชันการทำงานหลักของพวกเขา กระบวนการผสานรวม และตัวอย่างเชิงปฏิบัติ
ทำความเข้าใจเกี่ยวกับ MCP Servers และ OpenRouter
ในการผสานรวม MCP Servers กับ OpenRouter คุณต้องเข้าใจก่อนว่าแต่ละส่วนประกอบทำอะไร
OpenRouter: การเข้าถึงแบบรวมศูนย์สำหรับแบบจำลองภาษา
OpenRouter เป็นแพลตฟอร์มที่ช่วยลดความซับซ้อนในการโต้ตอบกับแบบจำลองภาษาขนาดใหญ่ (LLMs) จากผู้ให้บริการเช่น OpenAI, Anthropic และ xAI โดยมีจุดสิ้นสุด API เดียว https://openrouter.ai/api/v1/chat/completions
ที่เข้ากันได้กับโครงสร้าง API ของ OpenAI คุณสมบัติหลัก ได้แก่:
- Model Aggregation: เข้าถึง LLMs หลายร้อยรายการผ่านอินเทอร์เฟซเดียว
- Cost Optimization: กำหนดเส้นทางคำขอไปยังแบบจำลองที่คุ้มค่าตามความพร้อมใช้งานและราคา
- Load Balancing: กระจายคำขอเพื่อป้องกันการโอเวอร์โหลดบนผู้ให้บริการรายใดรายหนึ่ง
- Fallbacks: สลับไปยังแบบจำลองอื่นหากแบบจำลองหนึ่งล้มเหลว
คุณจะต้องมีบัญชี OpenRouter และคีย์ API เพื่อดำเนินการต่อ รับของคุณได้ที่ openrouter.ai

MCP Servers: การขยายขีดความสามารถของแบบจำลอง
MCP Servers ใช้ Model Context Protocol ทำให้ LLMs สามารถเรียกใช้เครื่องมือภายนอกได้ ซึ่งแตกต่างจากแบบจำลองแบบสแตนด์อโลนที่จำกัดอยู่แค่ข้อมูลการฝึกอบรม MCP Servers ช่วยให้สามารถโต้ตอบกับระบบต่างๆ เช่น ไดเรกทอรีไฟล์ ฐานข้อมูล หรือ APIs ของบุคคลที่สามได้แบบเรียลไทม์ คำจำกัดความของเครื่องมือ MCP ทั่วไปประกอบด้วย:
- Name: ระบุเครื่องมือ (เช่น
list_files
) - Description: อธิบายวัตถุประสงค์
- Parameters: ระบุอินพุตในรูปแบบ JSON Schema
ตัวอย่างเช่น เครื่องมือ MCP สำหรับแสดงรายการไฟล์ไดเรกทอรีอาจมีลักษณะดังนี้:
{
"name": "list_files",
"description": "Lists files in a specified directory",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Directory path"}
},
"required": ["path"]
}
}
เมื่อรวมกันแล้ว OpenRouter จะจัดเตรียมแบบจำลอง และ MCP Servers จะจัดหาเครื่องมือ ซึ่งก่อให้เกิดระบบนิเวศ AI ที่แข็งแกร่ง
เหตุใดจึงต้องผสานรวม MCP Servers กับ OpenRouter
การรวมเทคโนโลยีเหล่านี้เข้าด้วยกันมีข้อได้เปรียบทางเทคนิคหลายประการ:
- Model Flexibility: ใช้ LLM ที่รองรับ OpenRouter พร้อมความสามารถในการเรียกใช้เครื่องมือ
- Cost Reduction: จับคู่แบบจำลองที่ถูกกว่ากับเครื่องมือภายนอกเพื่อแบ่งเบาภาระงานที่ซับซ้อน
- Enhanced Capabilities: เปิดใช้งานแบบจำลองเพื่อดึงข้อมูลสดหรือดำเนินการ (เช่น การดำเนินการไฟล์)
- Scalability: สลับแบบจำลองหรือเครื่องมือโดยไม่ต้องเขียนตรรกะหลักของคุณใหม่
- Future-Proofing: ปรับให้เข้ากับ LLMs และเครื่องมือใหม่ๆ เมื่อมีการพัฒนา
การผสานรวมนี้เหมาะสำหรับนักพัฒนาที่สร้างระบบ AI ที่ต้องการการโต้ตอบกับโลกแห่งความเป็นจริง
กระบวนการผสานรวมทีละขั้นตอน
ตอนนี้ มาดูด้านเทคนิคกันบ้าง นี่คือวิธีการผสานรวม MCP Servers กับ OpenRouter
ข้อกำหนดเบื้องต้น
ตรวจสอบให้แน่ใจว่าคุณมี:
- OpenRouter Account and API Key: จาก openrouter.ai
- MCP Server: กำลังทำงานในเครื่อง (เช่น ที่
http://localhost:8000
) หรือจากระยะไกล สำหรับคำแนะนำนี้ ผมจะใช้เซิร์ฟเวอร์ MCP ระบบไฟล์ - Python 3.8+: พร้อมไลบรารี
requests
(pip install requests
) - Apidog: ไม่จำเป็น แต่แนะนำสำหรับการทดสอบ API ดาวน์โหลดฟรี
ขั้นตอนที่ 1: กำหนดและแปลงเครื่องมือ MCP
OpenRouter ใช้รูปแบบการเรียกใช้เครื่องมือของ OpenAI ดังนั้นคุณต้องแปลงคำจำกัดความของเครื่องมือ MCP เริ่มต้นด้วยคำจำกัดความ MCP:
{
"name": "list_files",
"description": "Lists files in a specified directory",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Directory path"}
},
"required": ["path"]
}
}
แปลงเป็นรูปแบบ OpenAI โดยเพิ่มฟิลด์ type
และซ้อนรายละเอียดฟังก์ชัน:
{
"type": "function",
"function": {
"name": "list_files",
"description": "Lists files in a specified directory",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Directory path"}
},
"required": ["path"]
}
}
}
โครงสร้าง JSON นี้คือสิ่งที่ OpenRouter คาดหวังในเพย์โหลด API
ขั้นตอนที่ 2: กำหนดค่าคำขอ API
เตรียมคำขอ API ไปยัง OpenRouter กำหนดส่วนหัวด้วยคีย์ API ของคุณและเพย์โหลดด้วยแบบจำลอง ข้อความ และเครื่องมือ นี่คือตัวอย่าง Python:
import requests
import json
# Headers
headers = {
"Authorization": "Bearer your_openrouter_api_key",
"Content-Type": "application/json"
}
# Payload
payload = {
"model": "openai/gpt-4", # Replace with your preferred model
"messages": [
{"role": "user", "content": "List files in the current directory."}
],
"tools": [
{
"type": "function",
"function": {
"name": "list_files",
"description": "Lists files in a specified directory",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Directory path"}
},
"required": ["path"]
}
}
}
]
}
แทนที่ your_openrouter_api_key
ด้วยคีย์จริงของคุณ
ขั้นตอนที่ 3: ส่งคำขอ API เริ่มต้น
ส่งคำขอ POST ไปยังจุดสิ้นสุดของ OpenRouter:
response = requests.post(
"https://openrouter.ai/api/v1/chat/completions",
headers=headers,
json=payload
)
response_data = response.json()
ขั้นตอนที่ 4: ประมวลผลการเรียกใช้เครื่องมือ
ตรวจสอบว่าการตอบสนองมีรายการเรียกใช้เครื่องมือหรือไม่:
{
"choices": [
{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_123",
"type": "function",
"function": {
"name": "list_files",
"arguments": "{\"path\": \".\"}"
}
}
]
}
}
]
}
แยกรายละเอียดการเรียกใช้เครื่องมือ:
message = response_data["choices"][0]["message"]
if "tool_calls" in message:
tool_call = message["tool_calls"][0]
function_name = tool_call["function"]["name"]
arguments = json.loads(tool_call["function"]["arguments"])
ขั้นตอนที่ 5: เรียกใช้ MCP Server
ส่งคำขอเครื่องมือไปยัง MCP Server ของคุณ:
mcp_response = requests.post(
"http://localhost:8000/call",
json={
"name": function_name,
"arguments": arguments
}
)
tool_result = mcp_response.json()["result"] # e.g., ["file1.txt", "file2.txt"]
ขั้นตอนที่ 6: ส่งคืนผลลัพธ์เครื่องมือไปยัง OpenRouter
ผนวกการเรียกใช้เครื่องมือของผู้ช่วยและผลลัพธ์ไปยังประวัติข้อความ:
messages = payload["messages"] + [
{
"role": "assistant",
"content": null,
"tool_calls": [tool_call]
},
{
"role": "tool",
"tool_call_id": tool_call["id"],
"content": json.dumps(tool_result)
}
]
# Update payload
payload["messages"] = messages
# Send follow-up request
final_response = requests.post(
"https://openrouter.ai/api/v1/chat/completions",
headers=headers,
json=payload
)
final_output = final_response.json()["choices"][0]["message"]["content"]
print(final_output) # e.g., "Files: file1.txt, file2.txt"
ขั้นตอนที่ 7: จัดการการเรียกใช้เครื่องมือหลายรายการ
หากแบบจำลองต้องการการเรียกใช้เครื่องมือหลายรายการ ให้วนซ้ำกระบวนการ:
messages = payload["messages"]
while True:
response = requests.post(
"https://openrouter.ai/api/v1/chat/completions",
headers=headers,
json={"model": "openai/gpt-4", "messages": messages}
)
message = response.json()["choices"][0]["message"]
if "tool_calls" not in message:
print(message["content"])
break
for tool_call in message["tool_calls"]:
function_name = tool_call["function"]["name"]
arguments = json.loads(tool_call["function"]["arguments"])
mcp_response = requests.post(
"http://localhost:8000/call",
json={"name": function_name, "arguments": arguments}
)
tool_result = mcp_response.json()["result"]
messages.extend([
{"role": "assistant", "content": null, "tool_calls": [tool_call]},
{"role": "tool", "tool_call_id": tool_call["id"], "content": json.dumps(tool_result)}
])
ซึ่งจะช่วยให้มั่นใจได้ว่าการเรียกใช้เครื่องมือทั้งหมดได้รับการประมวลผล
ตัวอย่างในโลกแห่งความเป็นจริง: การโต้ตอบกับระบบไฟล์
มาปรับใช้สิ่งนี้กับสถานการณ์จริงในการแสดงรายการไฟล์ด้วย MCP Server
- Tool Definition: ใช้เครื่องมือ
list_files
จากก่อนหน้านี้ - MCP Server: สมมติว่ากำลังทำงานที่
http://localhost:8000
- API Call: ส่ง “List files in the current directory” ไปยัง OpenRouter
- Response Handling: แบบจำลองเรียกใช้
list_files
ด้วย{"path": "."}
- MCP Execution: เซิร์ฟเวอร์ส่งคืน
["file1.txt", "file2.txt"]
- Final Output: แบบจำลองตอบสนองว่า “Files found: file1.txt, file2.txt.”
นี่คือโค้ดทั้งหมด:
import requests
import json
headers = {"Authorization": "Bearer your_openrouter_api_key", "Content-Type": "application/json"}
payload = {
"model": "openai/gpt-4",
"messages": [{"role": "user", "content": "List files in the current directory."}],
"tools": [{
"type": "function",
"function": {
"name": "list_files",
"description": "Lists files in a specified directory",
"parameters": {
"type": "object",
"properties": {"path": {"type": "string", "description": "Directory path"}},
"required": ["path"]
}
}
}]
}
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
message = response.json()["choices"][0]["message"]
if "tool_calls" in message:
tool_call = message["tool_calls"][0]
function_name = tool_call["function"]["name"]
arguments = json.loads(tool_call["function"]["arguments"])
mcp_response = requests.post("http://localhost:8000/call", json={"name": function_name, "arguments": arguments})
tool_result = mcp_response.json()["result"]
messages = payload["messages"] + [
{"role": "assistant", "content": null, "tool_calls": [tool_call]},
{"role": "tool", "tool_call_id": tool_call["id"], "content": json.dumps(tool_result)}
]
final_response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json={"model": "openai/gpt-4", "messages": messages})
print(final_response.json()["choices"][0]["message"]["content"])
การแก้ไขปัญหาทั่วไป
นี่คือวิธีแก้ไขปัญหาที่พบบ่อย:
- Tool Not Called: ตรวจสอบไวยากรณ์คำจำกัดความของเครื่องมือและความชัดเจนของพรอมต์
- Invalid Arguments: ตรวจสอบให้แน่ใจว่าพารามิเตอร์ตรงกับ JSON Schema
- MCP Server Failure: ตรวจสอบบันทึกเซิร์ฟเวอร์และจุดสิ้นสุด (
http://localhost:8000/call
) - API Authentication: ยืนยันว่าคีย์ OpenRouter ของคุณถูกต้อง
ใช้ Apidog เพื่อแก้ไขคำขอและคำตอบ API อย่างมีประสิทธิภาพ
การขยายการผสานรวม
หากต้องการเกิน 2000 คำและเพิ่มความลึก ให้พิจารณาการขยายเหล่านี้:
ตัวอย่างการสอบถามฐานข้อมูล
กำหนดเครื่องมือ MCP เพื่อสอบถามฐานข้อมูล:
{
"type": "function",
"function": {
"name": "query_db",
"description": "Queries a database with SQL",
"parameters": {
"type": "object",
"properties": {"sql": {"type": "string", "description": "SQL query"}},
"required": ["sql"]
}
}
}
ส่ง “Get all users from the database” ไปยัง OpenRouter ประมวลผลการเรียก query_db
และส่งคืนผลลัพธ์เช่น [{"id": 1, "name": "Alice"}]
การจัดการข้อผิดพลาด
เพิ่มการจัดการข้อผิดพลาดที่แข็งแกร่ง:
try:
mcp_response = requests.post("http://localhost:8000/call", json={"name": function_name, "arguments": arguments})
mcp_response.raise_for_status()
except requests.RequestException as e:
tool_result = f"Error: {str(e)}"
ซึ่งจะช่วยให้มั่นใจได้ว่าแอปพลิเคชันของคุณยังคงเสถียร
บทสรุป
การผสานรวม MCP Servers กับ OpenRouter ช่วยให้ AI ของคุณใช้ประโยชน์จากเครื่องมือภายนอกผ่าน API เดียว คำแนะนำนี้ครอบคลุมการตั้งค่า การแปลงเครื่องมือ การเรียก API และตัวอย่างเชิงปฏิบัติ เช่น การโต้ตอบกับระบบไฟล์ ด้วยประโยชน์ต่างๆ เช่น การประหยัดต้นทุนและฟังก์ชันการทำงานที่ได้รับการปรับปรุง แนวทางนี้เป็นสิ่งที่นักพัฒนาด้านเทคนิคต้องลอง
เริ่มทดลองเลย คว้า Apidog ได้ฟรี ที่นี่ เพื่อทดสอบ APIs ของคุณ แจ้งให้ผมทราบว่ามันเป็นอย่างไร!

```