Skip to main content
Welcome back. This lesson runs a small Python example that uses LangChain to drive an agent which interacts with an MCP server. The example shows how a simple prompt can cause the agent to pick MCP tools, perform a kubectl-like operation against a cluster, and return a human-friendly plain-text answer produced by the LLM.

What you’ll learn

  • How to start an MCP server container configured to talk to a local KinD cluster
  • How the sample agent discovers and invokes MCP tools
  • How the LLM converts raw tool output into a readable report

Setup

  1. Export the kubeconfig for the kind cluster and save it for use inside Docker:
  1. Start the MCP container so it can access the kind network. Replace IMAGE with your MCP server image name:

Files in this example

The script we run

Here’s the visible entrypoint script (query_agent.py). It builds a short prompt and calls into the MCP client to run the agent asynchronously.
This prompt is intentionally minimal to demonstrate the end-to-end flow. For production use, prefer structured prompt templates and explicit instructions so outputs are consistent and auditable.

High-level flow (what the script does)

  1. Builds the prompt (here: “List all namespaces”).
  2. Calls run_agent, which:
    • Connects to the configured MCP server.
    • Enumerates available tools exposed by the MCP server.
    • Selects an appropriate tool (for example, kubectl_get).
    • Executes the tool against the target cluster and captures the result (JSON or text).
    • Sends the raw tool result to the LLM to create a human-friendly report.
  3. Prints the final output to stdout.

Run the agent

From the project directory run:

Observed logs and output

Below is an example of the logs and the final LLM-generated answer observed when running the script against a local MCP listening on http://localhost:8000/mcp. These logs show connection setup, tool discovery, tool invocation, and the final LLM call.

Quick notes on the flow

  • The agent does not run a local kubectl in your environment. Instead, it invokes MCP tools (for example, kubectl_get) that run on the MCP server and operate against the cluster configured for that server.
  • Raw tool output is forwarded to the LLM, which generates the final human-friendly report printed by the script.
  • The LLM endpoint shown in the logs (e.g., https://api.ai.kodekloud.com/...) is environment-specific. Configure the LLM provider you use (OpenAI, Anthropic, a self-hosted model, etc.) in your environment.

Example MCP tools discovered

Next steps and practical extensions

  • For FinOps or governance agents, collect labels and annotations across workloads and services. Use structured prompts and scripts to aggregate tags and apply FinOps rules or policy checks.
  • Expand the agent’s prompt template to ask for specific data (e.g., costs, owner tags, environment labels) and produce CSV or JSON summaries suitable for automation.
  • Add unit tests or integration tests for mcp_client.py to validate tool selection and result parsing.
If you want, I can also:
  • Show an expanded mcp_client.py example demonstrating tool discovery and invocation.
  • Provide a more robust prompt template for production FinOps agents.

Watch Video

Practice Lab