---
contentId: 4c18ff8d-db49-4197-96f9-7ec16756e9cd
part: 3
title: Pair AI Agents with MCP to Access the Outside World
description: AI agents with custom tools are powerful. Connecting an MCP server to an agent unlocks another level of capability.
github:
  branch: 03.pairing-ai-agents-with-mcp-to-make-outside-world-accessible
date: 2025-10-07
seriesSlug: ai-agents-and-mcp-server-teaming-up-for-the-agentic-web
---

Our MCP server is ready, but the agent still does not know it exists. The next step is to give the agent an MCP client that can discover the server's tools and make them available to the model.

When we tested the server manually, the MCP Inspector used **List Tools** to retrieve its available tools. The AI SDK can do the same programmatically: it connects to the server, lists the tools, and adapts them to the interface used by `streamText`.

Before doing so, we need to add a Nitro runtime config to indicate the URL of the MCP.

```ts [Example] {6}
import { defineNitroConfig } from 'nitropack/config'

export default defineNitroConfig({
  runtimeConfig: {
    openAiApiKey: '',
    mcpEndpoint: '',
  },
  // ...
})
```

In your `.env` file, set the `MCP_ENDPOINT` variable to the MCP URL:

```ini [Example]
MCP_ENDPOINT=http://localhost:3000/mcp
```

Then, we can connect our agent to the MCP server. The client is created for the request, retrieves the available tools, and remains open until the streamed response finishes.

```ts [Example] {2,17-23,30-33}
import { createOpenAI } from '@ai-sdk/openai'
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
import { convertToModelMessages, experimental_createMCPClient, stepCountIs, streamText } from 'ai'
import { defineEventHandler, defineLazyEventHandler, readBody } from 'h3'
import { useRuntimeConfig } from 'nitropack/runtime'

export default defineLazyEventHandler(() => {
  const runtimeConfig = useRuntimeConfig()

  const model = createOpenAI({
    apiKey: runtimeConfig.openAiApiKey,
  })

  return defineEventHandler(async (event) => {
    const { messages } = await readBody(event)

    const httpTransport = new StreamableHTTPClientTransport(
      new URL(runtimeConfig.mcpEndpoint)
    )
    const httpClient = await experimental_createMCPClient({
      transport: httpTransport
    })
    const tools = await httpClient.tools()

    const result = streamText({
      model: model('gpt-5-nano'),
      system: `You are a helpful assistant. You can use the tool to add two numbers together.`,
      stopWhen: stepCountIs(2),
      tools,
      messages: convertToModelMessages(messages),
      onFinish: () => httpClient.close(),
      onAbort: () => httpClient.close(),
    })

    return result.toUIMessageStreamResponse()
  })
})
```

The most important line is `const tools = await httpClient.tools()`. Like the Inspector's **List Tools** action, it retrieves the tools exposed by the MCP server. Passing them to `streamText` makes those tools available to the model. When the model requests one, `experimental_createMCPClient` uses the HTTP transport to call the MCP server on the agent's behalf.



> [!NOTE]
> The MCP client uses the Streamable HTTP transport to exchange protocol messages with the MCP server. Closing the client in `onFinish` or `onAbort` keeps it available while the response is streaming and releases its resources afterward.



From the user's perspective, the agent behaves just as it did with a direct tool. But from the architectural perspective, the agent now consumes a standardized capability provided by a separate server.

<figure>
  <video autoplay loop muted playsinline>
    <source src="https://images.soubiran.dev/series/ai-agents-and-mcp-server-teaming-up-for-the-agentic-web/mcp-to-provide-additional-capabilities-to-the-ai-agent/ai-agent-paired-with-mcp.mp4" type="video/mp4">
  </video>
  <figcaption>The AI Agent using a tool and generating a response.</figcaption>
</figure>

The agent can now use a tool without knowing how it is implemented. We can evolve the MCP server independently, let compatible clients use it, or connect the agent to another MCP server when it needs additional capabilities. Next, we will add a chat interface so people can use the agent directly.
