How to connect a remote MCP server to the Gemini API

Day 6, 25 minutes · beginner

Goal of this lessonConnect a public MCP server to a Gemini request, read the MCP tool calls in the response and decide when MCP beats your own function.

Why it matters

So far every tool lived in your own script. That works for one app. It gets awkward when the same tool should serve several apps, or when someone else already runs a server that offers the tool you need. The Model Context Protocol (MCP) is an open standard for exactly that: a server lists its tools, and any client that speaks MCP can use them.

Today you let Gemini call a tool on a remote MCP server, with one setting in the request and no function runner of your own. You also see the other side of the deal: you are now trusting a server you did not write. You finish with a working call, and a short written rule for when Wilmot's desk should use MCP and when it should keep its own functions.

What "remote" means here

In the Interactions API, you add a tool of the type mcp_server to the request and give it a name and a URL. The response then contains the MCP tool calls and their results as extra steps, so the calls are made for you. Your code does not run a loop for these calls.

The docs only show public addresses, and say nothing about servers on your own computer, so treat a public address as the safe choice. This lesson does not ask you to host one. To use a server of your own you would need to deploy it somewhere public first. The MCP for beginners course builds a first server, and the protocol's own site at modelcontextprotocol.io explains the standard.

The fields, from the function calling page and the API reference:

  • type: always mcp_server.
  • name: a display name. Use letters and underscores, not a hyphen.
  • url: the full address of the server endpoint.
  • headers: optional, sent with every request, for example for authentication.
  • allowed_tools: optional, to restrict which of the server's tools Gemini may call.

Limits, as documented on 4 December 2026

  • Remote MCP only works with Streamable HTTP servers. SSE servers are not supported.
  • It is described for the Interactions API. The overview says Gemini 3 models do not support remote MCP, and the examples use Gemini 4 Hydrogen.
  • The function calling page does not label it a preview, but the API reference I read was the v1beta version, and Google changes this API often. Treat details as moving.
  • The docs do not say whether remote MCP has different limits on the free tier. If your call fails, read the error before assuming the course is wrong.

What comes back

The response has extra steps. A step of type mcp_server_tool_call has the server name, the tool name and the arguments Gemini sent. A mcp_server_tool_result step holds what the server returned. The final answer is still in interaction.output_text. The script prints the call steps, so you can see what Gemini asked the server.

The example in this lesson uses the demo weather server from Google's own docs, so you can try MCP without building anything. It is a demo: it may change or disappear, and it is not a real service for Skerrimoor. Never send private data to a server you do not know.

Trust and safety

An MCP server is code that other people run, and its answers go straight into the model's context. That has three consequences:

  • Tool results are input too. Text from a server can contain instructions that try to steer the model. Keep to servers you trust.
  • Secrets stay out of code. If a server needs a token, read it from an environment variable, as you did with the API key, and never write it into the script or the headers of a file you commit.
  • Start read-only. Prefer tools that only look things up. Use allowed_tools to leave out the rest. For anything that changes data, keep a person in the loop.

MCP or your own function?

  • Use your own function when the logic is yours, needs your private data, and only one app uses it. It is simpler and you control it.
  • Use a remote MCP server when the tool is shared or owned by someone else, and it already has a public, trusted address.
  • With functions, you run the loop and can check every call. With remote MCP, Gemini makes the calls on the server for you, so you check afterwards, through the steps.

The steps

  1. Write mcp_try.py with the demo server.
  2. Run it and find the MCP call line.
  3. Print every step type to see the shape of the response.
  4. Decide which of Wilmot's three tools could live behind a remote server, and write down your rule.

Pick your tool below and follow the steps.

Common mistakes

  • Using a hyphen in the server name.
  • Pointing at a server on your own computer.
  • Sending anything private to a demo server.
  • Assuming a failed call means your key is wrong. Read the error text first.

Today's actions

Ticks are not saved without an account

Do it in Gemini API

Gemini API

  1. Create mcp_try.py. It adds the demo weather server from Google's docs as an mcp_server tool and prints each MCP call Gemini made.
    from config import MODEL, client
    
    WEATHER_SERVER = {
        "type": "mcp_server",
        "name": "weather",  # snake_case, no hyphen
        "url": "https://gemini-api-demos.uc.r.appspot.com/mcp",
    }
    
    interaction = client.interactions.create(
        model=MODEL,
        input="Check the weather in San Francisco.",
        tools=[WEATHER_SERVER],
        generation_config={"thinking_level": "low", "max_output_tokens": 2000},
    )
    
    for step in interaction.steps:
        if step.type == "mcp_server_tool_call":
            print("MCP call:", step.server_name, step.name, step.arguments)
    print("status:", interaction.status)
    print(interaction.output_text)
    print("usage:", interaction.usage)
  2. Run python mcp_try.py. You should see a line starting MCP call: with the server name, the tool name and the arguments, then a short weather answer and the usage.
  3. Add this loop to the end of the script and run it again. It prints the type of every step, so you can see the shape of the response.
    for step in interaction.steps:
        print(step.type)
  4. Change the question to a city of your own and run again. Note whether the demo server answers. It is a demo and may change or disappear.
  5. Write mcp-or-function.md: three sentences saying which of Wilmot's three tools could live behind a remote MCP server, which should stay a function, and why.

Note: Remote MCP only works with Streamable HTTP servers, and the server name must not contain a hyphen. The server must be reachable on the internet, so a server on your own computer is not used here. Never send private data to a server you do not know, and keep tokens in environment variables.

Checked on 4 December 2026 · Source: Google AI Studio docs: Function calling (Remote MCP) (opens in a new tab)

Create a free account to save your progress. Sign in or sign up