1. บทนำ
ภาพรวม
ในแล็บนี้ คุณจะได้สร้างและติดตั้งใช้งานเซิร์ฟเวอร์ Model Context Protocol (MCP) เซิร์ฟเวอร์ MCP มีประโยชน์ในการให้สิทธิ์ LLM เข้าถึงเครื่องมือและบริการภายนอก คุณจะกำหนดค่าเป็นบริการที่ปลอดภัยและพร้อมใช้งานจริงใน Cloud Run ซึ่งเข้าถึงได้จากไคลเอ็นต์หลายราย จากนั้นคุณจะเชื่อมต่อกับเซิร์ฟเวอร์ MCP ระยะไกลจาก Gemini CLI
สิ่งที่คุณต้องดำเนินการ
เราจะใช้ FastMCP เพื่อสร้างเซิร์ฟเวอร์ MCP ของ Zoo ที่มีเครื่องมือ 2 อย่าง ได้แก่ get_animals_by_species และ get_animal_details FastMCP เป็นวิธีที่รวดเร็วและใช้ Python ในการสร้างเซิร์ฟเวอร์และไคลเอ็นต์ MCP

สิ่งที่คุณจะได้เรียนรู้
- ทำให้เซิร์ฟเวอร์ MCP ใช้งานได้ใน Cloud Run
- รักษาความปลอดภัยของปลายทางเซิร์ฟเวอร์โดยกำหนดให้มีการตรวจสอบสิทธิ์สำหรับคำขอทั้งหมด เพื่อให้มั่นใจว่าเฉพาะไคลเอ็นต์และตัวแทนที่ได้รับอนุญาตเท่านั้นที่จะสื่อสารกับเซิร์ฟเวอร์ได้
- เชื่อมต่อกับปลายทางเซิร์ฟเวอร์ MCP ที่ปลอดภัยจาก Gemini CLI
2. การตั้งค่าโปรเจ็กต์
- หากยังไม่มีบัญชี Google คุณต้องสร้างบัญชี Google
- ใช้บัญชีส่วนตัวแทนบัญชีงานหรือบัญชีโรงเรียน บัญชีงานและบัญชีโรงเรียนอาจมีข้อจำกัดที่ทำให้คุณเปิดใช้ API ที่จำเป็นสำหรับห้องทดลองนี้ไม่ได้
- ลงชื่อเข้าใช้ Google Cloud Console
- เปิดใช้การเรียกเก็บเงินใน Cloud Console
- การทำแล็บนี้ควรมีค่าใช้จ่ายน้อยกว่า $1 USD ในทรัพยากรระบบคลาวด์
- คุณสามารถทำตามขั้นตอนที่ส่วนท้ายของแล็บนี้เพื่อลบทรัพยากรเพื่อหลีกเลี่ยงการเรียกเก็บเงินเพิ่มเติม
- ผู้ใช้ใหม่มีสิทธิ์ใช้ช่วงทดลองใช้ฟรีมูลค่า$300 USD
- สร้างโปรเจ็กต์ใหม่หรือเลือกใช้โปรเจ็กต์ที่มีอยู่ซ้ำ
3. เปิดเครื่องมือแก้ไข Cloud Shell
- คลิกลิงก์นี้เพื่อไปยัง Cloud Shell Editor โดยตรง
- หากระบบแจ้งให้ให้สิทธิ์ในวันนี้ ให้คลิกให้สิทธิ์เพื่อดำเนินการต่อ

- หากเทอร์มินัลไม่ปรากฏที่ด้านล่างของหน้าจอ ให้เปิดโดยทำดังนี้
- คลิกดู
- คลิก Terminal

- ในเทอร์มินัล ให้ตั้งค่าโปรเจ็กต์ด้วยคำสั่งนี้
- รูปแบบ:
gcloud config set project [PROJECT_ID] - ตัวอย่าง
gcloud config set project lab-project-id-example - หากจำรหัสโปรเจ็กต์ไม่ได้ ให้ทำดังนี้
- คุณแสดงรหัสโปรเจ็กต์ทั้งหมดได้โดยใช้คำสั่งต่อไปนี้
gcloud projects list | awk '/PROJECT_ID/{print $2}'

- คุณแสดงรหัสโปรเจ็กต์ทั้งหมดได้โดยใช้คำสั่งต่อไปนี้
- รูปแบบ:
- คุณควรเห็นข้อความต่อไปนี้
หากเห็นUpdated property [core/project].
WARNINGและระบบขอให้คุณDo you want to continue (Y/n)?แสดงว่าคุณอาจป้อนรหัสโปรเจ็กต์ไม่ถูกต้อง กดnกดEnterแล้วลองเรียกใช้คำสั่งgcloud config set projectอีกครั้ง
4. เปิดใช้ API
เปิดใช้ API ในเทอร์มินัลโดยทำดังนี้
gcloud services enable \
run.googleapis.com \
artifactregistry.googleapis.com \
cloudbuild.googleapis.com
หากได้รับแจ้งให้ให้สิทธิ์ ให้คลิกให้สิทธิ์เพื่อดำเนินการต่อ 
คำสั่งนี้อาจใช้เวลาสักครู่จึงจะเสร็จสมบูรณ์ แต่ในที่สุดควรจะแสดงข้อความว่าสำเร็จคล้ายกับข้อความนี้
Operation "operations/acf.p2-73d90d00-47ee-447a-b600" finished successfully.
5. เตรียมโปรเจ็กต์ Python
- สร้างโฟลเดอร์ชื่อ
mcp-on-cloudrunเพื่อจัดเก็บซอร์สโค้ดสำหรับการติดตั้งใช้งานmkdir mcp-on-cloudrun && cd mcp-on-cloudrun - สร้างโปรเจ็กต์ Python ด้วยเครื่องมือ
uvเพื่อสร้างไฟล์pyproject.tomlดังนี้ คำสั่งuv init --description "Example of deploying an MCP server on Cloud Run" --bare --python 3.13uv initจะสร้างไฟล์pyproject.tomlสำหรับโปรเจ็กต์ของคุณ หากต้องการดูเนื้อหาของไฟล์ ให้เรียกใช้คำสั่งต่อไปนี้ เอาต์พุตควรมีลักษณะดังนี้cat pyproject.toml[project] name = "mcp-on-cloudrun" version = "0.1.0" description = "Example of deploying an MCP server on Cloud Run" requires-python = ">=3.13" dependencies = []
6. สร้างเซิร์ฟเวอร์ MCP ของ Zoo
หากต้องการให้บริบทที่มีคุณค่าในการปรับปรุงการใช้ LLM กับ MCP ให้ตั้งค่าเซิร์ฟเวอร์ MCP ของ Zoo ด้วย FastMCP ซึ่งเป็นเฟรมเวิร์กมาตรฐานสำหรับการทำงานกับ Model Context Protocol FastMCP เป็นวิธีที่รวดเร็วในการสร้างเซิร์ฟเวอร์และไคลเอ็นต์ MCP ด้วย Python เซิร์ฟเวอร์ MCP นี้ให้ข้อมูลเกี่ยวกับสัตว์ในสวนสัตว์สมมติ เราจัดเก็บข้อมูลไว้ในหน่วยความจำเพื่อให้ง่ายต่อการใช้งาน สำหรับเซิร์ฟเวอร์ MCP ที่ใช้งานจริง คุณอาจต้องการระบุข้อมูลจากแหล่งที่มา เช่น ฐานข้อมูลหรือ API
- เรียกใช้คำสั่งต่อไปนี้เพื่อเพิ่ม FastMCP เป็นการขึ้นต่อกันในไฟล์
pyproject.toml การดำเนินการนี้จะเพิ่มไฟล์uv add fastmcp==2.12.4 --no-syncuv.lockลงในโปรเจ็กต์ - สร้างและเปิด
server.pyไฟล์ใหม่สำหรับซอร์สโค้ดเซิร์ฟเวอร์ MCP โดยทำดังนี้ คำสั่งcloudshell edit ~/mcp-on-cloudrun/server.pycloudshell editจะเปิดไฟล์server.pyในเครื่องมือแก้ไขเหนือเทอร์มินัล - เพิ่มซอร์สโค้ดเซิร์ฟเวอร์ MCP ของ Zoo ต่อไปนี้ในไฟล์
server.pyimport asyncio import logging import os from typing import List, Dict, Any from fastmcp import FastMCP logger = logging.getLogger(__name__) logging.basicConfig(format="[%(levelname)s]: %(message)s", level=logging.INFO) mcp = FastMCP("Zoo Animal MCP Server 🦁🐧🐻") # Dictionary of animals at the zoo ZOO_ANIMALS = [ { "species": "lion", "name": "Leo", "age": 7, "enclosure": "The Big Cat Plains", "trail": "Savannah Heights" }, { "species": "lion", "name": "Nala", "age": 6, "enclosure": "The Big Cat Plains", "trail": "Savannah Heights" }, { "species": "lion", "name": "Simba", "age": 3, "enclosure": "The Big Cat Plains", "trail": "Savannah Heights" }, { "species": "lion", "name": "King", "age": 8, "enclosure": "The Big Cat Plains", "trail": "Savannah Heights" }, { "species": "penguin", "name": "Waddles", "age": 2, "enclosure": "The Arctic Exhibit", "trail": "Polar Path" }, { "species": "penguin", "name": "Pip", "age": 4, "enclosure": "The Arctic Exhibit", "trail": "Polar Path" }, { "species": "penguin", "name": "Skipper", "age": 5, "enclosure": "The Arctic Exhibit", "trail": "Polar Path" }, { "species": "penguin", "name": "Chilly", "age": 3, "enclosure": "The Arctic Exhibit", "trail": "Polar Path" }, { "species": "penguin", "name": "Pingu", "age": 6, "enclosure": "The Arctic Exhibit", "trail": "Polar Path" }, { "species": "penguin", "name": "Noot", "age": 1, "enclosure": "The Arctic Exhibit", "trail": "Polar Path" }, { "species": "elephant", "name": "Ellie", "age": 15, "enclosure": "The Pachyderm Sanctuary", "trail": "Savannah Heights" }, { "species": "elephant", "name": "Peanut", "age": 12, "enclosure": "The Pachyderm Sanctuary", "trail": "Savannah Heights" }, { "species": "elephant", "name": "Dumbo", "age": 5, "enclosure": "The Pachyderm Sanctuary", "trail": "Savannah Heights" }, { "species": "elephant", "name": "Trunkers", "age": 10, "enclosure": "The Pachyderm Sanctuary", "trail": "Savannah Heights" }, { "species": "bear", "name": "Smokey", "age": 10, "enclosure": "The Grizzly Gulch", "trail": "Polar Path" }, { "species": "bear", "name": "Grizzly", "age": 8, "enclosure": "The Grizzly Gulch", "trail": "Polar Path" }, { "species": "bear", "name": "Barnaby", "age": 6, "enclosure": "The Grizzly Gulch", "trail": "Polar Path" }, { "species": "bear", "name": "Bruin", "age": 12, "enclosure": "The Grizzly Gulch", "trail": "Polar Path" }, { "species": "giraffe", "name": "Gerald", "age": 4, "enclosure": "The Tall Grass Plains", "trail": "Savannah Heights" }, { "species": "giraffe", "name": "Longneck", "age": 5, "enclosure": "The Tall Grass Plains", "trail": "Savannah Heights" }, { "species": "giraffe", "name": "Patches", "age": 3, "enclosure": "The Tall Grass Plains", "trail": "Savannah Heights" }, { "species": "giraffe", "name": "Stretch", "age": 6, "enclosure": "The Tall Grass Plains", "trail": "Savannah Heights" }, { "species": "antelope", "name": "Speedy", "age": 2, "enclosure": "The Tall Grass Plains", "trail": "Savannah Heights" }, { "species": "antelope", "name": "Dash", "age": 3, "enclosure": "The Tall Grass Plains", "trail": "Savannah Heights" }, { "species": "antelope", "name": "Gazelle", "age": 4, "enclosure": "The Tall Grass Plains", "trail": "Savannah Heights" }, { "species": "antelope", "name": "Swift", "age": 5, "enclosure": "The Tall Grass Plains", "trail": "Savannah Heights" }, { "species": "polar bear", "name": "Snowflake", "age": 7, "enclosure": "The Arctic Exhibit", "trail": "Polar Path" }, { "species": "polar bear", "name": "Blizzard", "age": 5, "enclosure": "The Arctic Exhibit", "trail": "Polar Path" }, { "species": "polar bear", "name": "Iceberg", "age": 9, "enclosure": "The Arctic Exhibit", "trail": "Polar Path" }, { "species": "walrus", "name": "Wally", "age": 10, "enclosure": "The Walrus Cove", "trail": "Polar Path" }, { "species": "walrus", "name": "Tusker", "age": 12, "enclosure": "The Walrus Cove", "trail": "Polar Path" }, { "species": "walrus", "name": "Moby", "age": 8, "enclosure": "The Walrus Cove", "trail": "Polar Path" }, { "species": "walrus", "name": "Flippers", "age": 9, "enclosure": "The Walrus Cove", "trail": "Polar Path" } ] @mcp.tool() def get_animals_by_species(species: str) -> List[Dict[str, Any]]: """ Retrieves all animals of a specific species from the zoo. Can also be used to collect the base data for aggregate queries of animals of a specific species - like counting the number of penguins or finding the oldest lion. Args: species: The species of the animal (e.g., 'lion', 'penguin'). Returns: A list of dictionaries, where each dictionary represents an animal and contains details like name, age, enclosure, and trail. """ logger.info(f">>> 🛠️ Tool: 'get_animals_by_species' called for '{species}'") return [animal for animal in ZOO_ANIMALS if animal["species"].lower() == species.lower()] @mcp.tool() def get_animal_details(name: str) -> Dict[str, Any]: """ Retrieves the details of a specific animal by its name. Args: name: The name of the animal. Returns: A dictionary with the animal's details (species, name, age, enclosure, trail) or an empty dictionary if the animal is not found. """ logger.info(f">>> 🛠️ Tool: 'get_animal_details' called for '{name}'") for animal in ZOO_ANIMALS: if animal["name"].lower() == name.lower(): return animal return {} if __name__ == "__main__": port = int(os.getenv("PORT", 8080)) logger.info(f"🚀 MCP server started on port {port}") asyncio.run( mcp.run_async( transport="http", host="0.0.0.0", port=port, ) )
รหัสของคุณเสร็จสมบูรณ์แล้ว ถึงเวลาติดตั้งใช้งานเซิร์ฟเวอร์ MCP ใน Cloud Run แล้ว
7. การทำให้ใช้งานได้กับ Cloud Run
ตอนนี้คุณสามารถทำให้เซิร์ฟเวอร์ MCP ใช้งานได้ใน Cloud Run โดยตรงจากซอร์สโค้ด
- สร้างและเปิด
Dockerfileใหม่เพื่อนำไปใช้งานใน Cloud Run โดยทำดังนี้cloudshell edit ~/mcp-on-cloudrun/Dockerfile - ใส่โค้ดต่อไปนี้ใน Dockerfile เพื่อใช้เครื่องมือ
uvในการเรียกใช้ไฟล์server.py# Use the official Python image FROM python:3.13-slim # Install uv COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ # Install the project into /app COPY . /app WORKDIR /app # Allow statements and log messages to immediately appear in the logs ENV PYTHONUNBUFFERED=1 # Install dependencies RUN uv sync EXPOSE $PORT # Run the FastMCP server CMD ["uv", "run", "server.py"] - สร้างบัญชีบริการชื่อ
mcp-server-saโดยทำดังนี้gcloud iam service-accounts create mcp-server-sa --display-name="MCP Server Service Account" - เรียกใช้
gcloudคำสั่งเพื่อติดตั้งใช้งานแอปพลิเคชันใน Cloud Run ใช้แฟล็กcd ~/mcp-on-cloudrun gcloud run deploy zoo-mcp-server \ --service-account=mcp-server-sa@$GOOGLE_CLOUD_PROJECT.iam.gserviceaccount.com \ --no-allow-unauthenticated \ --region=europe-west1 \ --source=. \ --labels=dev-tutorial=codelab-mcp--no-allow-unauthenticatedเพื่อกำหนดให้มีการตรวจสอบสิทธิ์ ซึ่งเป็นสิ่งสำคัญเนื่องจากเหตุผลด้านความปลอดภัย หากคุณไม่ต้องการการตรวจสอบสิทธิ์ ทุกคนจะโทรหาเซิร์ฟเวอร์ MCP ของคุณได้และอาจทำให้ระบบเสียหาย - ยืนยันการสร้างที่เก็บ Artifact Registry ใหม่ เนื่องจากเป็นการติดตั้งใช้งานจากซอร์สโค้ดไปยัง Cloud Run เป็นครั้งแรก คุณจะเห็นสิ่งต่อไปนี้
พิมพ์Deploying from source requires an Artifact Registry Docker repository to store built containers. A repository named [cloud-run-source-deploy] in region [europe-west1] will be created. Do you want to continue (Y/n)?
Yแล้วกดEnterเพื่อสร้างที่เก็บ Artifact Registry สำหรับการติดตั้งใช้งาน ต้องมีบัญชีนี้เพื่อจัดเก็บคอนเทนเนอร์ Docker ของเซิร์ฟเวอร์ MCP สำหรับบริการ Cloud Run - หลังจากนั้นไม่กี่นาที คุณจะเห็นข้อความต่อไปนี้
Service [zoo-mcp-server] revision [zoo-mcp-server-12345-abc] has been deployed and is serving 100 percent of traffic.
คุณได้ติดตั้งใช้งานเซิร์ฟเวอร์ MCP แล้ว ตอนนี้คุณใช้ได้แล้ว
8. เพิ่มเซิร์ฟเวอร์ MCP ระยะไกลไปยัง Gemini CLI
ตอนนี้คุณได้ติดตั้งใช้งานเซิร์ฟเวอร์ MCP ระยะไกลเรียบร้อยแล้ว คุณสามารถเชื่อมต่อกับเซิร์ฟเวอร์ดังกล่าวได้โดยใช้แอปพลิเคชันต่างๆ เช่น Google Code Assist หรือ Gemini CLI ในส่วนนี้ เราจะสร้างการเชื่อมต่อกับเซิร์ฟเวอร์ MCP ระยะไกลใหม่โดยใช้ Gemini CLI
- ให้สิทธิ์บัญชีผู้ใช้ในการเรียกเซิร์ฟเวอร์ MCP ระยะไกล
gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT \ --member=user:$(gcloud config get-value account) \ --role='roles/run.invoker' - บันทึกข้อมูลเข้าสู่ระบบ Google Cloud และหมายเลขโปรเจ็กต์ในตัวแปรสภาพแวดล้อมเพื่อใช้ในไฟล์การตั้งค่า Gemini โดยทำดังนี้
export PROJECT_NUMBER=$(gcloud projects describe $GOOGLE_CLOUD_PROJECT --format="value(projectNumber)") export ID_TOKEN=$(gcloud auth print-identity-token) - สร้างโฟลเดอร์
.geminiหากยังไม่ได้สร้างmkdir -p ~/.gemini - เปิดไฟล์การตั้งค่า Gemini CLI
cloudshell edit ~/.gemini/settings.json - แทนที่ไฟล์การตั้งค่า Gemini CLI เพื่อเพิ่มเซิร์ฟเวอร์ MCP ของ Cloud Run
{ "ide": { "hasSeenNudge": true }, "mcpServers": { "zoo-remote": { "httpUrl": "https://zoo-mcp-server-$PROJECT_NUMBER.europe-west1.run.app/mcp", "headers": { "Authorization": "Bearer $ID_TOKEN" } } }, "security": { "auth": { "selectedType": "cloud-shell" } } } - เริ่มใช้ Gemini CLI ใน Cloud Shell
คุณอาจต้องกดgeminiEnterเพื่อยอมรับการตั้งค่าเริ่มต้นบางอย่าง
- ให้ Gemini แสดงรายการเครื่องมือ MCP ที่พร้อมใช้งานในบริบทของ Gemini
/mcp - ขอให้ Gemini ค้นหาสิ่งใดสิ่งหนึ่งในสวนสัตว์
Gemini CLI ควรทราบวิธีใช้เซิร์ฟเวอร์Where can I find penguins?zoo-remoteMCP และจะถามว่าคุณต้องการอนุญาตให้ดำเนินการ MCP หรือไม่ - ใช้ลูกศรลง แล้วกด
Enterเพื่อเลือกYes, always allow all tools from server "zoo-remote"

เอาต์พุตควรแสดงคำตอบที่ถูกต้องและกล่องแสดงที่ระบุว่าใช้เซิร์ฟเวอร์ MCP

คุณทำได้แล้ว คุณได้ติดตั้งใช้งานเซิร์ฟเวอร์ MCP ระยะไกลใน Cloud Run และทดสอบโดยใช้ Gemini CLI เรียบร้อยแล้ว
เมื่อพร้อมที่จะสิ้นสุดเซสชัน ให้พิมพ์ /quit แล้วกด Enter เพื่อออกจาก Gemini CLI
การแก้ไขข้อบกพร่อง
หากเห็นข้อผิดพลาดดังนี้
🔍 Attempting OAuth discovery for 'zoo-remote'... ❌ 'zoo-remote' requires authentication but no OAuth configuration found Error connecting to MCP server 'zoo-remote': MCP server 'zoo-remote' requires authentication. Please configure OAuth or check server settings.
โทเค็นรหัสอาจหมดเวลาแล้วและต้องตั้งค่า ID_TOKEN อีกครั้ง
- พิมพ์
/quitแล้วกดEnterเพื่อออกจาก Gemini CLI - ตั้งค่าโปรเจ็กต์ในเทอร์มินัล
gcloud config set project [PROJECT_ID] - เริ่มใหม่ในขั้นตอนที่ 2 ด้านบน
9. (ไม่บังคับ) ยืนยันการเรียกใช้เครื่องมือในบันทึกของเซิร์ฟเวอร์
หากต้องการยืนยันว่ามีการเรียกเซิร์ฟเวอร์ MCP ของ Cloud Run ให้ตรวจสอบบันทึกของบริการ
gcloud run services logs read zoo-mcp-server --region europe-west1 --limit=5
คุณควรเห็นบันทึกเอาต์พุตที่ยืนยันว่ามีการเรียกใช้เครื่องมือ 🛠️
2025-08-05 19:50:31 INFO: 169.254.169.126:39444 - "POST /mcp HTTP/1.1" 200 OK 2025-08-05 19:50:31 [INFO]: Processing request of type CallToolRequest 2025-08-05 19:50:31 [INFO]: >>> 🛠️ Tool: 'get_animals_by_species' called for 'penguin'
10. (ไม่บังคับ) เพิ่มพรอมต์ MCP ลงในเซิร์ฟเวอร์
พรอมต์ MCP จะช่วยเร่งเวิร์กโฟลว์สำหรับพรอมต์ที่คุณเรียกใช้บ่อยๆ ได้ด้วยการสร้างคำย่อสำหรับพรอมต์ที่ยาวขึ้น
Gemini CLI จะแปลงพรอมต์ MCP เป็นคําสั่ง Slash ที่กําหนดเองโดยอัตโนมัติเพื่อให้คุณเรียกใช้พรอมต์ MCP ได้โดยพิมพ์ /prompt_name โดยที่ prompt_name คือชื่อของพรอมต์ MCP
สร้างพรอมต์ MCP เพื่อให้คุณค้นหาสัตว์ในสวนสัตว์ได้อย่างรวดเร็วโดยพิมพ์ /find animal ลงใน Gemini CLI
- เพิ่มโค้ดนี้ลงในไฟล์
server.pyเหนือการ์ดหลัก (if __name__ == "__main__":)@mcp.prompt() def find(animal: str) -> str: """ Find which exhibit and trail a specific animal might be located. """ return ( f"Please find the exhibit and trail information for {animal} in the zoo. " f"Respond with '[animal] can be found in the [exhibit] on the [trail].'" f"Example: Penguins can be found in The Arctic Exhibit on the Polar Path." ) - ทำให้แอปพลิเคชันใช้งานได้อีกครั้งใน Cloud Run
gcloud run deploy zoo-mcp-server \ --region=europe-west1 \ --source=. \ --labels=dev-tutorial=codelab-mcp - รีเฟรช ID_TOKEN สำหรับเซิร์ฟเวอร์ MCP ระยะไกล
export ID_TOKEN=$(gcloud auth print-identity-token) - หลังจากที่ติดตั้งใช้งานแอปพลิเคชันเวอร์ชันใหม่แล้ว ให้เริ่ม Gemini CLI
gemini - ในพรอมต์ ให้ใช้คำสั่งที่กำหนดเองใหม่ที่คุณสร้างขึ้น
หรือ/find --animal="lions"/find lions
คุณควรเห็นว่า Gemini CLI เรียกใช้get_animals_by_speciesเครื่องมือและจัดรูปแบบคำตอบตามที่ได้รับคำสั่งจากพรอมต์ MCP
╭───────────────────────────╮
│ > /find --animal="lion" │
╰───────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✔ get_animals_by_species (zoo-remote MCP Server) get_animals_by_species (zoo-remote MCP Server) │
│ │
│ [{"species":"lion","name":"Leo","age":7,"enclosure":"The Big Cat │
│ Plains","trail":"Savannah │
│ Heights"},{"species":"lion","name":"Nala","age":6,"enclosure":"The Big Cat │
│ Plains","trail":"Savannah │
│ Heights"},{"species":"lion","name":"Simba","age":3,"enclosure":"The Big Cat │
│ Plains","trail":"Savannah │
│ Heights"},{"species":"lion","name":"King","age":8,"enclosure":"The Big Cat │
│ Plains","trail":"Savannah Heights"}] │
╰───────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ Lions can be found in The Big Cat Plains on the Savannah Heights.
11. (ไม่บังคับ) ใช้ Gemini Flash Lite เพื่อรับคำตอบที่เร็วขึ้น
Gemini CLI ช่วยให้คุณเลือกโมเดลที่ใช้ได้
- Gemini 2.5 Pro เป็นโมเดลการคิดสุดล้ำของ Google ที่สามารถให้เหตุผลเกี่ยวกับปัญหาที่ซับซ้อนในโค้ด คณิตศาสตร์ และ STEM รวมถึงวิเคราะห์ชุดข้อมูล ฐานของโค้ด และเอกสารขนาดใหญ่โดยใช้บริบทแบบยาว
- Gemini 2.5 Flash เป็นโมเดลที่ดีที่สุดของ Google ในแง่ของราคาต่อประสิทธิภาพ โดยมีขีดความสามารถที่ครอบคลุม 2.5 Flash เหมาะที่สุดสำหรับการประมวลผลขนาดใหญ่ งานที่มีเวลาในการตอบสนองต่ำและมีปริมาณมากซึ่งต้องใช้ความคิด และ Use Case ของเอเจนต์
- Gemini 2.5 Flash Lite เป็นโมเดล Flash ที่เร็วที่สุดของ Google ซึ่งได้รับการเพิ่มประสิทธิภาพเพื่อความคุ้มค่าและมีปริมาณงานสูง
เนื่องจากคำขอที่เกี่ยวข้องกับการค้นหาสัตว์ในสวนสัตว์ไม่จำเป็นต้องใช้การคิดหรือการให้เหตุผล ให้ลองเพิ่มความเร็วด้วยการใช้โมเดลที่เร็วกว่า
สร้างพรอมต์ MCP เพื่อให้คุณค้นหาสัตว์ในสวนสัตว์ได้อย่างรวดเร็วโดยพิมพ์ /find animal ลงใน Gemini CLI
- หลังจากที่ติดตั้งใช้งานแอปพลิเคชันเวอร์ชันใหม่แล้ว ให้เริ่ม Gemini CLI
gemini --model=gemini-2.5-flash-lite - ในพรอมต์ ให้ใช้คำสั่งที่กำหนดเองใหม่ที่คุณสร้างขึ้น
/find lions
คุณจะยังเห็นว่า Gemini CLI เรียกใช้เครื่องมือ get_animals_by_species และจัดรูปแบบคำตอบตามที่พรอมต์ MCP สั่ง แต่คำตอบจะปรากฏเร็วขึ้นมาก
╭───────────────────────────╮
│ > /find --animal="lion" │
╰───────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✔ get_animals_by_species (zoo-remote MCP Server) get_animals_by_species (zoo-remote MCP Server) │
│ │
│ [{"species":"lion","name":"Leo","age":7,"enclosure":"The Big Cat │
│ Plains","trail":"Savannah │
│ Heights"},{"species":"lion","name":"Nala","age":6,"enclosure":"The Big Cat │
│ Plains","trail":"Savannah │
│ Heights"},{"species":"lion","name":"Simba","age":3,"enclosure":"The Big Cat │
│ Plains","trail":"Savannah │
│ Heights"},{"species":"lion","name":"King","age":8,"enclosure":"The Big Cat │
│ Plains","trail":"Savannah Heights"}] │
╰───────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ Lions can be found in The Big Cat Plains on the Savannah Heights.
การแก้ไขข้อบกพร่อง
หากเห็นข้อผิดพลาดดังนี้
✕ Unknown command: /find --animal="lions"
ลองเรียกใช้ /mcp และหากแสดงผล zoo-remote - Disconnected คุณอาจต้องติดตั้งใช้งานอีกครั้ง หรือเรียกใช้คำสั่งต่อไปนี้อีกครั้ง
gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT \
--member=user:$(gcloud config get-value account) \
--role='roles/run.invoker'
export PROJECT_NUMBER=$(gcloud projects describe $GOOGLE_CLOUD_PROJECT --format="value(projectNumber)")
export ID_TOKEN=$(gcloud auth print-identity-token)
12. บทสรุป
ยินดีด้วย คุณได้ติดตั้งใช้งานและเชื่อมต่อกับเซิร์ฟเวอร์ MCP ระยะไกลที่ปลอดภัยเรียบร้อยแล้ว
ไปยัง Lab ถัดไป
ห้องทดลองนี้เป็นห้องทดลองแรกในชุด 3 ตอน ในแล็บที่ 2 คุณจะใช้เซิร์ฟเวอร์ MCP ที่สร้างด้วยตัวแทน ADK
ใช้เซิร์ฟเวอร์ MCP ใน Cloud Run กับเอเจนต์ ADK
(ไม่บังคับ) ล้างข้อมูล
หากไม่ต้องการทำแล็บถัดไปและต้องการล้างข้อมูลที่สร้างไว้ คุณสามารถลบโปรเจ็กต์ Cloud เพื่อหลีกเลี่ยงค่าใช้จ่ายเพิ่มเติม
แม้ว่า Cloud Run จะไม่เรียกเก็บเงินเมื่อไม่ได้ใช้บริการ แต่คุณอาจยังคงถูกเรียกเก็บเงินสำหรับการจัดเก็บอิมเมจคอนเทนเนอร์ใน Artifact Registry การลบโปรเจ็กต์ Cloud จะหยุดการเรียกเก็บเงินสำหรับทรัพยากรทั้งหมดที่ใช้ภายในโปรเจ็กต์นั้น
หากต้องการ ให้ลบโปรเจ็กต์โดยทำดังนี้
gcloud projects delete $GOOGLE_CLOUD_PROJECT
นอกจากนี้ คุณอาจต้องการลบทรัพยากรที่ไม่จำเป็นออกจากดิสก์ Cloud Shell ด้วย ดังนี้
- ลบไดเรกทอรีโปรเจ็กต์ Codelab
rm -rf ~/mcp-on-cloudrun - คำเตือน! การดำเนินการถัดไปนี้จะยกเลิกไม่ได้ หากต้องการลบทุกอย่างใน Cloud Shell เพื่อเพิ่มพื้นที่ว่าง คุณสามารถลบไดเรกทอรีหน้าแรกทั้งหมดได้ โปรดระมัดระวังและตรวจสอบว่าได้บันทึกทุกอย่างที่คุณต้องการเก็บไว้ที่อื่นแล้ว
sudo rm -rf $HOME