วิธีทำให้เซิร์ฟเวอร์ MCP ที่ปลอดภัยใช้งานได้ใน Cloud Run

1. บทนำ

ภาพรวม

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

สิ่งที่คุณต้องดำเนินการ

เราจะใช้ FastMCP เพื่อสร้างเซิร์ฟเวอร์ MCP ของสวนสัตว์ที่มีเครื่องมือ 2 อย่าง ได้แก่ get_animals_by_species และ get_animal_details FastMCP เป็นวิธีที่รวดเร็วและใช้ Python ในการสร้างเซิร์ฟเวอร์และไคลเอ็นต์ MCP

กราฟิกเซิร์ฟเวอร์ Zoo MCP

สิ่งที่คุณจะได้เรียนรู้

  • ทำให้เซิร์ฟเวอร์ MCP ใช้งานได้ใน Cloud Run
  • รักษาความปลอดภัยของปลายทางเซิร์ฟเวอร์โดยกำหนดให้มีการตรวจสอบสิทธิ์สำหรับคำขอทั้งหมด เพื่อให้มั่นใจว่าเฉพาะไคลเอ็นต์และตัวแทนที่ได้รับอนุญาตเท่านั้นที่จะสื่อสารกับเซิร์ฟเวอร์ได้
  • เชื่อมต่อกับปลายทางเซิร์ฟเวอร์ MCP ที่ปลอดภัยจาก Gemini CLI

2. การตั้งค่าโปรเจ็กต์

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

3. เปิดเครื่องมือแก้ไข Cloud Shell

  1. คลิกลิงก์นี้เพื่อไปยัง Cloud Shell Editor โดยตรง
  2. หากระบบแจ้งให้ให้สิทธิ์ในวันนี้ ให้คลิกให้สิทธิ์เพื่อดำเนินการต่อ คลิกเพื่อให้สิทธิ์ Cloud Shell
  3. หากเทอร์มินัลไม่ปรากฏที่ด้านล่างของหน้าจอ ให้เปิดโดยทำดังนี้
    • คลิกดู
    • คลิก Terminalเปิดเทอร์มินัลใหม่ใน Cloud Shell Editor
  4. ในเทอร์มินัล ให้ตั้งค่าโปรเจ็กต์ด้วยคำสั่งนี้
    • รูปแบบ:
      gcloud config set project [PROJECT_ID]
      
    • ตัวอย่าง
      gcloud config set project lab-project-id-example
      
    • หากจำรหัสโปรเจ็กต์ไม่ได้ ให้ทำดังนี้
      • คุณแสดงรหัสโปรเจ็กต์ทั้งหมดได้โดยใช้คำสั่งต่อไปนี้
        gcloud projects list | awk '/PROJECT_ID/{print $2}'
        
      ตั้งค่ารหัสโปรเจ็กต์ในเทอร์มินัล Cloud Shell Editor
  5. คุณควรเห็นข้อความต่อไปนี้
    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

หากได้รับแจ้งให้ให้สิทธิ์ ให้คลิกให้สิทธิ์เพื่อดำเนินการต่อ คลิกเพื่อให้สิทธิ์ Cloud Shell

คำสั่งนี้อาจใช้เวลาสักครู่จึงจะเสร็จสมบูรณ์ แต่ในที่สุดควรจะแสดงข้อความว่าดำเนินการสำเร็จคล้ายกับข้อความนี้

Operation "operations/acf.p2-73d90d00-47ee-447a-b600" finished successfully.

5. เตรียมโปรเจ็กต์ Python

  1. สร้างโฟลเดอร์ชื่อ mcp-on-cloudrun เพื่อจัดเก็บซอร์สโค้ดสำหรับการติดตั้งใช้งาน
    mkdir mcp-on-cloudrun && cd mcp-on-cloudrun
    
  2. สร้างโปรเจ็กต์ Python ด้วยเครื่องมือ uv เพื่อสร้างไฟล์ pyproject.toml ดังนี้
    uv init --description "Example of deploying an MCP server on Cloud Run" --bare --python 3.13
    
    คำสั่ง uv 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

  1. เรียกใช้คำสั่งต่อไปนี้เพื่อเพิ่ม FastMCP เป็นการขึ้นต่อกันในไฟล์ pyproject.toml
    uv add fastmcp==2.12.4 --no-sync
    
    การดำเนินการนี้จะเพิ่มไฟล์ uv.lock ลงในโปรเจ็กต์
  2. สร้างและเปิดserver.pyไฟล์ใหม่สำหรับซอร์สโค้ดเซิร์ฟเวอร์ MCP โดยทำดังนี้
    cloudshell edit server.py
    
    คำสั่ง cloudshell edit จะเปิดไฟล์ server.py ในเครื่องมือแก้ไขเหนือเทอร์มินัล
  3. เพิ่มซอร์สโค้ดเซิร์ฟเวอร์ MCP ของ Zoo ต่อไปนี้ในไฟล์ server.py
    import 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 โดยตรงจากซอร์สโค้ด

  1. สร้างและเปิด Dockerfile ใหม่เพื่อนำไปใช้งานใน Cloud Run โดยทำดังนี้
    cloudshell edit Dockerfile
    
  2. ใส่โค้ดต่อไปนี้ใน 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"]
    
  3. เรียกใช้gcloudคำสั่งเพื่อติดตั้งใช้งานแอปพลิเคชันใน Cloud Run
    gcloud run deploy zoo-mcp-server \
        --no-allow-unauthenticated \
        --region=europe-west1 \
        --source=. \
        --labels=dev-tutorial=codelab-mcp
    
    ใช้แฟล็ก --no-allow-unauthenticated เพื่อกำหนดให้มีการตรวจสอบสิทธิ์ การดำเนินการนี้มีไว้เพื่อความปลอดภัย หากคุณไม่ต้องการการตรวจสอบสิทธิ์ ทุกคนจะโทรหาเซิร์ฟเวอร์ MCP ของคุณได้และอาจทำให้ระบบเสียหาย
  4. ยืนยันการสร้างที่เก็บ 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
  5. หลังจากนั้นไม่กี่นาที คุณจะเห็นข้อความต่อไปนี้
    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

  1. ให้สิทธิ์บัญชีผู้ใช้ในการเรียกเซิร์ฟเวอร์ MCP ระยะไกล
    gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT \
        --member=user:$(gcloud config get-value account) \
        --role='roles/run.invoker'
    
  2. บันทึกข้อมูลเข้าสู่ระบบ Google Cloud และหมายเลขโปรเจ็กต์ในตัวแปรสภาพแวดล้อมเพื่อใช้ในไฟล์การตั้งค่า Gemini โดยทำดังนี้
    export PROJECT_NUMBER=$(gcloud projects describe $GOOGLE_CLOUD_PROJECT --format="value(projectNumber)")
    export ID_TOKEN=$(gcloud auth print-identity-token)
    
  3. เปิดไฟล์การตั้งค่า Gemini CLI
    cloudshell edit ~/.gemini/settings.json
    
  4. แทนที่ไฟล์การตั้งค่า Gemini CLI เพื่อเพิ่มเซิร์ฟเวอร์ MCP ของ Cloud Run
    {
      "mcpServers": {
        "zoo-remote": {
          "httpUrl": "https://zoo-mcp-server-$PROJECT_NUMBER.europe-west1.run.app/mcp",
          "headers": {
            "Authorization": "Bearer $ID_TOKEN"
          }
        }
      },
      "selectedAuthType": "cloud-shell",
      "hasSeenIdeIntegrationNudge": true
    }
    

  1. เริ่มใช้ Gemini CLI ใน Cloud Shell
    gemini
    
    คุณอาจต้องกด Enter เพื่อยอมรับการตั้งค่าเริ่มต้นบางอย่างมุมมองเริ่มต้นของ Gemini CLI
  2. ให้ Gemini แสดงรายการเครื่องมือ MCP ที่พร้อมใช้งานในบริบทของตัวเอง
    /mcp
    
  3. ขอให้ Gemini ค้นหาสิ่งใดสิ่งหนึ่งในสวนสัตว์
    Where can I find penguins?
    
    Gemini CLI ควรทราบวิธีใช้เซิร์ฟเวอร์ zoo-remote MCP และจะถามว่าคุณต้องการอนุญาตให้ดำเนินการ MCP หรือไม่
  4. ใช้ลูกศรลง แล้วกด Enter เพื่อเลือก
    Yes, always allow all tools from server "zoo-remote"
    
    Gemini CLI อนุญาตเครื่องมือระยะไกลของ Zoo

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

Gemini CLI show zoo mcp server result

คุณทำได้แล้ว คุณได้ติดตั้งใช้งานเซิร์ฟเวอร์ 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 อีกครั้ง

  1. พิมพ์ /quit แล้วกด Enter เพื่อออกจาก Gemini CLI
  2. ตั้งค่าโปรเจ็กต์ในเทอร์มินัล
    gcloud config set project [PROJECT_ID]
    
  3. เริ่มใหม่ในขั้นตอนที่ 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

  1. เพิ่มโค้ดนี้ลงในไฟล์ 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."
        )
    
  2. ทำให้แอปพลิเคชันใช้งานได้อีกครั้งใน Cloud Run
    gcloud run deploy zoo-mcp-server \
        --no-allow-unauthenticated \
        --region=europe-west1 \
        --source=. \
        --labels=dev-tutorial=codelab-mcp
    
  3. รีเฟรช ID_TOKEN สำหรับเซิร์ฟเวอร์ MCP ระยะไกล
    export ID_TOKEN=$(gcloud auth print-identity-token)
    
  4. หลังจากที่ติดตั้งใช้งานแอปพลิเคชันเวอร์ชันใหม่แล้ว ให้เริ่ม Gemini CLI
    gemini
    
  5. ในพรอมต์ ให้ใช้คำสั่งที่กำหนดเองใหม่ที่คุณสร้างขึ้น
    /find --animal="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.

เป้าหมายที่ท้าทายเพื่อทดสอบตัวเอง

หากต้องการเพิ่มความท้าทาย ให้ลองทำตามขั้นตอนเดียวกันเพื่อสร้างพรอมต์สำหรับแสดงข้อเท็จจริงสนุกๆ เกี่ยวกับสัตว์สายพันธุ์ใดสายพันธุ์หนึ่งในสวนสัตว์

หรือหากต้องการทดสอบสิ่งที่ได้เรียนรู้ให้มากขึ้นไปอีก ให้คิดไอเดียเครื่องมือที่คุณจะใช้บ่อยๆ แล้วติดตั้งใช้งานเซิร์ฟเวอร์ MCP ระยะไกลเครื่องที่ 2 จากนั้นเพิ่มลงในการตั้งค่า Gemini CLI เพื่อดูว่าใช้งานได้หรือไม่

การแก้ไขข้อบกพร่อง

หากเห็นข้อผิดพลาดดังนี้

✕ Unknown command: /find --animal="lions"

ลองเรียกใช้ /mcp และหากแสดงผลเป็น zoo-remote - Disconnected คุณอาจต้องติดตั้งใช้งานอีกครั้ง หรือเรียกใช้คำสั่งต่อไปนี้อีกครั้ง

```shell
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)
```

11. บทสรุป

ยินดีด้วย คุณได้ติดตั้งใช้งานและเชื่อมต่อกับเซิร์ฟเวอร์ MCP ระยะไกลที่ปลอดภัยเรียบร้อยแล้ว

ไปยัง Lab ถัดไป

ห้องทดลองนี้เป็นห้องทดลองแรกในชุด 3 ตอน ในแล็บที่ 2 คุณจะใช้เซิร์ฟเวอร์ MCP ที่สร้างด้วยตัวแทน ADK

ใช้เซิร์ฟเวอร์ MCP ใน Cloud Run กับเอเจนต์ ADK

(ไม่บังคับ) ล้างข้อมูล

หากไม่ต้องการทำแล็บถัดไปและต้องการล้างข้อมูลที่สร้างไว้ คุณสามารถลบโปรเจ็กต์ Cloud เพื่อหลีกเลี่ยงค่าใช้จ่ายเพิ่มเติม

แม้ว่า Cloud Run จะไม่เรียกเก็บเงินเมื่อไม่ได้ใช้บริการ แต่คุณอาจยังคงถูกเรียกเก็บเงินสำหรับการจัดเก็บอิมเมจคอนเทนเนอร์ใน Artifact Registry การลบโปรเจ็กต์ Cloud จะหยุดการเรียกเก็บเงินสำหรับทรัพยากรทั้งหมดที่ใช้ภายในโปรเจ็กต์นั้น

หากต้องการ ให้ลบโปรเจ็กต์โดยทำดังนี้

gcloud projects delete $GOOGLE_CLOUD_PROJECT

นอกจากนี้ คุณอาจต้องการลบทรัพยากรที่ไม่จำเป็นออกจากดิสก์ Cloud Shell ด้วย ดังนี้

  1. ลบไดเรกทอรีโปรเจ็กต์ Codelab
    rm -rf ~/mcp-on-cloudrun
    
  2. คำเตือน! การดำเนินการถัดไปนี้จะยกเลิกไม่ได้ หากต้องการลบทุกอย่างใน Cloud Shell เพื่อเพิ่มพื้นที่ว่าง คุณสามารถลบทั้งไดเรกทอรีหน้าแรกได้ โปรดระมัดระวังและตรวจสอบว่าได้บันทึกทุกอย่างที่คุณต้องการเก็บไว้ที่อื่นแล้ว
    sudo rm -rf $HOME