Generative AI

Step-by-by-step codes to describe the custom model's veteran server

In this study of Colob – we are ready, we show how we can combine Geminative Gemenative Ai produced with a MCP model protocol (MCP), using FastMCP. Interactive GetPass Prompt to capture your Gemini_api_kely safely, we install and prepare all MCP dependence on the Open-Meteo Weatho Weathion, and St_asyncio Calling Asyns. Job crack continues to the Fastmcpp server “for the Getitude, Longitude, Fastmcpmitum, Famini Hemisini, we have a call based on our clear Jon Schema, and then make the drop-out Jons Schema. with MCP Client, to retrieve our MCP data.

from getpass import getpass
import os


api_key = getpass("Enter your GEMINI_API_KEY: ")
os.environ["GEMINI_API_KEY"] = api_key

We move you to save your Gemini API key (without showing it on the screen) and keep it in Gemini_ap_apy's change, allowing your entire account to authenticate with Google's API.

!pip install -q google-genai mcp fastmcp httpx nest_asyncio

We include the basic dependence that is required for our Colab Notebook in Google-Genai Google Communication API Gemino API and HTPP and our Cocks at Outside Apis, Our AsynC Code works well.

We use Nest_asyncio Pet in the existing NoteBook LOOP, allowing us to use asyncio Cootines (such as MCP customer communication) without meeting “Events Errors Active” Errors.

from fastmcp import FastMCP
import httpx


mcp_server = FastMCP("weather")


@mcp_server.tool()
def get_weather(latitude: float, longitude: float) -> str:
    """3‑day min/max temperature forecast via Open‑Meteo."""
    url = (
        f"
        f"?latitude={latitude}&longitude={longitude}"
        "&daily=temperature_2m_min,temperature_2m_max&timezone=UTC"
    )
    resp = httpx.get(url, timeout=10)
    daily = resp.json()["daily"]
    return "n".join(
        f"{date}: low {mn}°C, high {mx}°C"
        for date, mn, mx in zip(
            daily["time"],
            daily["temperature_2m_min"],
            daily["temperature_2m_max"],
        )
    )


@mcp_server.tool()
def get_alerts(state: str) -> str:
    """Dummy US‑state alerts."""
    return f"No active weather alerts for {state.upper()}."

We create a Fastmcp server for the Fastmcp process called “Weather” and registered two tools: Get

import asyncio
from google import genai
from google.genai import types
from fastmcp import Client as MCPClient
from fastmcp.client.transports import FastMCPTransport

We import important libraries of our MCP-GEME: Asynchronous's libraries, Google-Geniai and its client Gemmcptransport to link our MCP weather server.

client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
MODEL = "gemini-2.0-flash"
transport = FastMCPTransport(mcp_server)

We start using Google Gemini client using Gemini_api_key from your environment, explain the Gemini-2.0-flash model to call the work, and set Fastmcpptransport linking MCP_SeverVer Client for MCP.

function_declarations = [
    {
        "name": "get_weather",
        "description": "Return a 3‑day min/max temperature forecast for given coordinates.",
        "parameters": {
            "type": "object",
            "properties": {
                "latitude": {
                    "type": "number",
                    "description": "Latitude of target location."
                },
                "longitude": {
                    "type": "number",
                    "description": "Longitude of target location."
                }
            },
            "required": ["latitude", "longitude"]
        }
    },
    {
        "name": "get_alerts",
        "description": "Return any active weather alerts for a given U.S. state.",
        "parameters": {
            "type": "object",
            "properties": {
                "state": {
                    "type": "string",
                    "description": "Two‑letter U.S. state code, e.g. 'CA'."
                }
            },
            "required": ["state"]
        }
    }
]


tool_defs = types.Tool(function_declarations=function_declarations)

We manually explain JSON Schema's clarification with our tools for two MCPs, find and fold these programs to the types. Toolbar (Tools_Defs), informing Gemini not to produce and verify related work calls.

async def run_gemini(lat: float, lon: float):
    async with MCPClient(transport) as mcp_client:
        prompt = f"Give me a 3‑day weather forecast for latitude={lat}, longitude={lon}."
        response = client.models.generate_content(
            model=MODEL,
            contents=[prompt],
            config=types.GenerateContentConfig(
                temperature=0,
                tools=[tool_defs]
            )
        )


        call = response.candidates[0].content.parts[0].function_call
        if not call:
            print("No function call; GPT said:", response.text)
            return


        print("🔧 Gemini wants:", call.name, call.args)


        result = await mcp_client.call_tool(call.name, call.args)
        print("n📋 Tool result:n", result)


asyncio.get_event_loop().run_until_complete(run_gemini(37.7749, -122.4194))

Finally, the Async Run_gemini work opens a MCP client over our operating system, sends a speedy gemini requesting 3 days, and can be kicked by the Low Incort, all bills by molding

In Consusion, We Have a Fully Contained Pipeline That Showcases How To Define McP Tools in Python, Expose Them Via Them.0 Model Sing Google-Genai Client. Key components, FASTMCP for MCP, FASTMCPRPRANPORT AND MCPCLIENT OF MCPCLIENT, NEEDCIO COLOB, work together to enable real-time work without external processes or stdio processes. This amount is simple for local development and MCP integration from Colob and provides a template for developing integrated integrated applications that include special display of domain tools.


Here is the Colab Notebook. Also, don't forget to follow Sane and join ours Telegraph station including LinkedIn Grtopic. Don't forget to join ours 90k + ml subreddit.

🔥 [Register Now] Summit of the Minicon Virtual in Agentic AI: Free Registration + Certificate of Before Hour 4 Hour Court (May 21, 9 AM


Asphazzaq is a Markteach Media Inc. According to a View Business and Developer, Asifi is committed to integrating a good social intelligence. His latest attempt is launched by the launch of the chemistrylife plan for an intelligence, MarktechPost, a devastating intimate practice of a machine learning and deep learning issues that are clearly and easily understood. The platform is adhering to more than two million moon visits, indicating its popularity between the audience.

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button