Adding MCP Tools to Your First Kagent Agent
MCP (Model Context Protocol) tools extend the agent's abilities by calling external services to perform tasks that require logic or access outside the cluster.
In this guide, you'll learn how to add an MCP tool to your first AI agent using the kagent resources.
Prerequisites
Before you begin make sure you have a Kubernetes cluster with kagent installed. If you haven't done this yet, check out the installation guide or the quickstart guide.
We'll be working with the kagent custom resources, so make sure you have them present on your cluster:
kubectl get crd | grep kagent.dev
Creating the agent
Similar to the first agent guide, we will first create a simple agent that can interact with the cluster. This time we will use the Agent
resource to create the agent.
kubectl apply -f - <<EOFapiVersion: kagent.dev/v1alpha1kind: Agentmetadata:name: simple-k8s-agentnamespace: kagentspec:description: This agent can interact with the Kubernetes API to get information about the cluster.modelConfigRef: default-model-configsystemMessage: |-You're a friendly and helpful agent that uses Kubernetes tools to answer user questions about the cluster.# Instructions- If user question is unclear, ask for clarification before running any tools- Always be helpful and friendly- If you don't know how to answer the question DO NOT make things uprespond with "Sorry, I don't know how to answer that" and ask the user to further clarify the question# Response format- ALWAYS format your response as Markdown- Your response will include a summary of actions you took and an explanation of the resulttools:- inline:provider: kagent.tools.k8s.GetAvailableAPIResourcestype: Inline- inline:provider: kagent.tools.k8s.GetResourcestype: InlineEOF
Note that the way you structure your instructions is up to you. You can add more details, or simplify them as needed. It's important to make sure the instructions are clear and easy to follow.
We'll leave the default model (GPT-4o) selected and move to the next step.
Adding tools
Tools are the other building block of the agent. They are the commands that the agent can run to interact with the environment. As LLMs don't have the ability to run commands, tools are the way to bridge the gap between the agent and the environment. Kagent provides a set of built-in tools that you can use to interact with Kubernetes, Istio, Prometheus and projects. You can also build your own tools!
In our Agent resource, we added two tools:
GetResources
, which will enable the agent to run kubectl get command and retrieve resources running in the cluster.GetAvailableAPIResources
, which will enable the agent to get a list of available API resources in the cluster.
Test out the agent
Let's test out the agent by asking it a question. First we need to open the kagent dashboard and select the agent we just created. We can either use the kagent
CLI tool to open the dashboard or port-forward manually:
kubectl -n kagent port-forward svc/kagent 8001:80

In the chat interface, type "What pods are running in the cluster?" and press enter. The agent should respond with a list of pods running in the cluster and a call using the GetResources
tool.

Configuring MCP tools
Let's create an agent that uses the MCP tools to retrieve information from a website using the Kagent resources. For this example, we will use a simple MCP example server that only has one tool - the fetch tool. The fetch tool will take a URL as input and return the contents of the page.
First let's apply a simple MCP server that will run in our cluster:
kubectl apply -f - <<EOFapiVersion: apps/v1kind: Deploymentmetadata:name: mcp-website-fetcherlabels:app: mcp-website-fetcherspec:replicas: 1selector:matchLabels:app: mcp-website-fetchertemplate:metadata:labels:app: mcp-website-fetcherspec:containers:- name: mcp-website-fetcherimage: ghcr.io/peterj/mcp-website-fetcher:sha-d2db5b3imagePullPolicy: IfNotPresentports:- containerPort: 8000resources:limits:cpu: "500m"memory: "256Mi"requests:cpu: "100m"memory: "128Mi"livenessProbe:httpGet:path: /sseport: 8000initialDelaySeconds: 10periodSeconds: 30---apiVersion: v1kind: Servicemetadata:name: mcp-website-fetcherspec:selector:app: mcp-website-fetcherports:- port: 80targetPort: 8000type: ClusterIPEOF
Now in addition to the Agent
resource, we need to define a ToolServer
resource:
kubectl apply -f - <<EOFapiVersion: kagent.dev/v1alpha1kind: Agentmetadata:name: simple-fetch-agentnamespace: kagentspec:description: This agent can use a single tool to retrieve the contents of a webpage.modelConfigRef: default-model-configsystemMessage: |-You're a friendly and helpful agent that uses the fetch tool to retrieve webpage contents.# Instructions- If user question is unclear, ask for clarification before running any tools- Always be helpful and friendly- If you don't know how to answer the question DO NOT make things uprespond with "Sorry, I don't know how to answer that" and ask the user to further clarify the question# Response format- ALWAYS format your response as Markdown- Your response will include a summary of actions you took and an explanation of the resulttools:- type: McpServermcpServer:toolServer: mcp-toolservertoolNames:- fetch---apiVersion: kagent.dev/v1alpha1kind: ToolServermetadata:name: mcp-toolservernamespace: kagentspec:description: Fetches a website and returns its contentconfig:sse:url: http://mcp-website-fetcher.kagent.svc.cluster.local/sseEOF
MCP tools require a little more additional configuration before they can be used by agents.
You will notice that we're configuring the MCP server by pointing to the URL of the service we just deployed. The tool section defines the tool that the agent will use. In this case, we'll use a tool called fetch that takes a single input parameter - the URL to fetch. The input schema is discovered from the tool itself so you do not need to specify it.
Note that MCP servers can implement multiple tools and as an agent developer you can decide which tools to use.
Testing the agent with the MCP tool
Now let's try our agent out with the new integration. Open the kagent dashboard and select the agent we just created. You should see a new tool appear in the tool list.
Now let's ask our agent a question. Type "Show me the contents of the https://en.wikipedia.org/wiki/Service_mesh website?" and press enter. The agent should respond with the contents of the website.

Next Steps
- Learn more about Core Concepts
- Join our Community