In this lesson, you’ll learn how to make your agent dramatically more powerful by connecting it to external tool servers using the Model Context Protocol (MCP).MCP allows an agent to dynamically discover and use new tools without requiring any changes to its own code. We’ll connect our agent to public servers that provide tools for searching AWS documentation, web search, and accommodation booking, turning our general-purpose agent into a specialized expert.
The bridge between your agent and external tool servers. It’s configured to start servers using uvx or npx, which downloads and runs packages without manual installation.
Tools fetched from servers are passed directly to the Agent constructor. The agent learns about tools and their functions at runtime, without needing prior knowledge.
# Set up MCP client to connect to AWS documentation servermcp_client = MCPClient( lambda: stdio_client( StdioServerParameters( command="uvx", args=["awslabs.aws-documentation-mcp-server@latest"] ) ))
The uvx command automatically downloads and runs the MCP server package. No manual installation needed!
# Create agent with AWS documentation toolswith mcp_client: aws_tools = mcp_client.list_tools_sync() print(f"Successfully loaded {len(aws_tools)} tools from the MCP server.") agent = Agent( model=model, tools=aws_tools, system_prompt=( "You are an expert on Amazon Web Services. " "Use the provided tools to answer questions about AWS services " "based on the official documentation. Always provide accurate, " "up-to-date information from the AWS docs." ), ) # Query the agent user_query = "What is the maximum invocation payload size for AWS Lambda?" print("\n--- Querying AWS Documentation ---") print(f"User Query: {user_query}\n") response = agent(user_query) print("--- Agent Response ---") print(response)
Successfully loaded 3 tools from the MCP server.--- Querying AWS Documentation ---User Query: What is the maximum invocation payload size for AWS Lambda?--- Agent Response ---According to the AWS Lambda documentation, the maximum invocation payload size (request and response) is 6 MB for synchronous invocations and 256 KB for asynchronous invocations.
The agent used the MCP tools to search the AWS documentation and provided an accurate, up-to-date answer!
import logginglogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)try: # Validate configuration if not os.getenv("NEBIUS_API_KEY"): raise ValueError("NEBIUS_API_KEY is required") # Set up MCP client with mcp_client: tools = mcp_client.list_tools_sync() logger.info(f"Loaded {len(tools)} tools") # Create and use agent agent = Agent(model=model, tools=tools) response = agent(query)except ConnectionError as e: logger.error(f"Failed to connect to MCP server: {e}")except ValueError as e: logger.error(f"Configuration error: {e}")except Exception as e: logger.error(f"Unexpected error: {e}")
system_prompt = """You are a DevOps expert with access to AWS documentation and GitHub.When answering questions:1. First check AWS docs for service information2. Then search GitHub for implementation examples3. Provide complete, actionable answers"""
Experiment 3: Inspect Available Tools
See what tools are available:
with mcp_client: tools = mcp_client.list_tools_sync() for tool in tools: print(f"Tool: {tool.name}") print(f"Description: {tool.description}") print(f"Parameters: {tool.input_schema}\n")
Your agent can now access external tools! But what if you need human oversight for critical decisions? In the next lesson, you’ll learn how to implement human-in-the-loop patterns where agents can pause and request human approval.
Lesson 05: Human-in-the-Loop
Learn how to add human approval and oversight to your agent workflows