Station 8 · Building Agents · Hands-on (code)
Write a real AI agent in Python with Google ADK that calls a live REST API and calls an MCP server to solve a problem — all free on Gemini. Every line is verified to run.
Downloadable workbook
Open Colab → File → Upload notebook → run top to bottom. Prefer to follow along by hand? The full steps are below.
One agent (an LLM brain) with two kinds of hands: a REST API tool for live data, and an MCP server for your own private tools/data.
A free Gemini API key (no credit card) and a browser. That's it — google-adk installs in Colab and brings Gemini + MCP support with it.
Part A
Install Google's Agent Development Kit, then point it at the free AI Studio API with your key.
!pip install google-genai google-adk import os, getpass os.environ['GOOGLE_API_KEY'] = getpass.getpass('Paste your Gemini key: ').strip() os.environ['GOOGLE_GENAI_USE_VERTEXAI'] = 'FALSE' # use the free AI Studio API
Add a tiny helper that runs an agent and returns its reply (ADK is asynchronous — await works in Colab cells):
from google.adk.runners import InMemoryRunner from google.genai import types MODEL = 'gemini-2.0-flash' async def run_agent(agent, prompt, user='student'): runner = InMemoryRunner(agent=agent, app_name='lab') session = await runner.session_service.create_session(app_name='lab', user_id=user) msg = types.Content(role='user', parts=[types.Part(text=prompt)]) final = '' async for event in runner.run_async(user_id=user, session_id=session.id, new_message=msg): if event.is_final_response() and event.content: final = event.content.parts[0].text or final return final
Part B
An ADK tool is just a Python function with a docstring. This one calls a free, no-key currency API. Hand it to an Agent and the model decides when to call it.
import requests from google.adk.agents import Agent def convert_currency(amount: float, base: str, target: str) -> dict: """Convert money using live rates. base/target like 'USD','EUR'.""" url = f'https://api.frankfurter.app/latest?amount={amount}&from={base}&to={target}' data = requests.get(url, timeout=10).json() return {'result': data['rates'].get(target.upper())} api_agent = Agent(name='money', model=MODEL, tools=[convert_currency], instruction='For any conversion you MUST call convert_currency — never guess.') print(await run_agent(api_agent, 'How much is 50 US dollars in euros?'))
Part C
MCP is the "USB-C for AI tools" from Station 7. First, write your own tiny MCP server — a Python file exposing private "team" data:
from mcp.server.fastmcp import FastMCP mcp = FastMCP('team') ROLES = {'maya': 'Team Lead', 'leo': 'Data Analyst', 'ada': 'Designer'} @mcp.tool() def get_member_role(name: str) -> str: """Look up a team member's role by first name.""" return ROLES.get(name.lower(), 'Unknown') if __name__ == '__main__': mcp.run(transport='stdio')
Then connect it to an agent with MCPToolset. ADK launches the server, discovers its tools, and lets the agent call them:
from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset from google.adk.tools.mcp_tool.mcp_session_manager import StdioConnectionParams from mcp import StdioServerParameters team_tools = MCPToolset(connection_params=StdioConnectionParams( server_params=StdioServerParameters(command='python3', args=['team_server.py']))) mcp_agent = Agent(name='team', model=MODEL, tools=[team_tools], instruction='Use the MCP tools as your only source of team info.') print(await run_agent(mcp_agent, "What is Maya's role?"))
Part D
Put the API tool and the MCP server on one agent, and it'll use each where needed — in a single run.
assistant = Agent(name='assistant', model=MODEL, tools=[convert_currency, team_tools], instruction='Use MCP tools for team info and convert_currency for money. Use tools, never guess.') print(await run_agent(assistant, "Maya earned a 500 USD bonus. What's her role, and how much is 500 USD in euros?"))
Colab is for learning; Replit is for building. Put the agent in agent.py, add GOOGLE_API_KEY in Replit Secrets, and run adk web — you get a chat UI that shows every tool call and a shareable link.
Model error? Swap to gemini-2.5-flash. MCP cell hangs? Re-run the %%writefile cell first. Version note: built & verified on google-adk 2.x / mcp 1.x.
You did it. You built an agent in code that makes real API and MCP calls to solve a problem — with a production framework, free on Gemini. Its no-code sibling is next.