Skip to main content
This guide shows you how to let users dynamically enable/disable OpenAI Connectors and MCP servers per conversation or message, similar to how ChatGPT and Claude let you toggle tools on and off.

When to Use Dynamic Tools

Use dynamic tools when you want users to control which capabilities are available. This helps reduce agent bloat by allowing you to assign tools to an agent on demand rather than deploying lots of agents where each one has a different permutation of tools. For agents that should always have specific capabilities, pass the tools directly via the agent’s toolkits parameter. This works for both MCP tools and MeshAgent toolkits. See Using OpenAI Connectors & MCP Servers for examples of creating agents with always-on MCP toolkits.

How Dynamic Tools Work

Dynamic tool selection requires three components: (1) deployed services that expose MCP Servers or OpenAI Connectors, (2) an agent configured to accept MCP tools, and (3) a UI that displays available services and lets users toggle them on/off.

1. Deployed Services

First you need to package and deploy a project or room service for the MCP Servers or OpenAI Connectors you want the agent to have access to. You can do this by defining a service yaml file and creating the service using the MeshAgent CLI (see examples below).

2. Agent with MCPToolkitBuilder

Your agent needs to be configured with MCPToolkitBuilder() so it can accept and use the MCP tools that users select. The CLI ChatBot does this automatically:
bash
meshagent chatbot join --room=myroom --agent-name=agent --mcp
The --mcp flag adds MCPToolkitBuilder() for you behind the scenes.
Note: The CLI ChatBot also supports other dynamic toolkits like --web-search and --storage that work similarly. They add toolkit builders so users can enable/disable these capabilities per message.
For custom ChatBots, you must add it manually:
python
from meshagent.openai.tools.responses_adapter import MCPToolkitBuilder

class MyChatBot(ChatBot):
    async def get_thread_toolkit_providers(self, *, thread_context, participant):
        providers = await super().get_thread_toolkit_providers(
            thread_context=thread_context,
            participant=participant
        )
        providers.append(MCPToolkitBuilder())  # Add this!
        return providers

3. UI configured to discover and display services

Your UI needs to discover and display available services so users can toggle them on/off. MeshAgent Studio handles this automatically - when you open a room with a ChatBot that has MCP enabled, it queries for available services and shows them as selectable tools. If you’re building a custom UI, you’ll need to implement similar discovery and selection functionality.

How They Work Together

When a user sends a message:
  1. The UI displays available services and passes the user’s selected services to the ChatBot
  2. The ChatBot’s MCPToolkitBuilder creates tool definitions with the server endpoints
  3. These definitions are sent to OpenAI’s Responses API via the OpenAIResponsesAdapter
  4. OpenAI connects to each MCP server and discovers what tools they offer
  5. The agent can now call those tools, and the adapter handles execution and returning the results
Important: MeshAgent Studio + CLI ChatBot with --mcp flag handles the UI hooks and MCP setup automatically. This lets you quickly test dynamic MCP tools without writing any UI code or creating a custom agent yourself. When you’re ready to customize, you can build your own agent or UI by implementing the same pattern.

Example 1: Create a MeshAgent Service for an MCP Server

Let’s deploy the DeepWiki MCP Server which provides tools for reading public GitHub repositories (no authentication required). Step 1: Create the Service YAML
yaml
kind: Service
version: v1
metadata:
  name: mcp-deepwiki
  description: "Expose DeepWiki MCP server"
ports:
- num: 443                 # 443 for HTTPS external services; use 80 for HTTP
  type: http
  endpoints:
  - path: /mcp             # external.url + path are appended together
    mcp:
      label: "mcp-deepwiki"
      description: "MCP DeepWiki Tools"
external:
  url: "https://mcp.deepwiki.com"
Step 2: Deploy the Service
meshagent service create --file="meshagent.yaml"  # optional: --room=myroom
Step 3: Start the CLI ChatBot with MCP Enabled
meshagent chatbot join --room=myroom --agent-name=agent --mcp
# make sure room matches room you deployed to if deployed as a room service
Step 4: Use It in MeshAgent Studio Open MeshAgent Studio, go to myroom, select the chatbot, turn on MCP tools, and toggle “mcp-deepwiki”. Then you can ask questions like “What’s in the README for pydantic-ai?”. If you don’t want to use the DeepWiki tools anymore you can simply toggle them off.

Example 2: Create a MeshAgent Service for an OpenAI Connector

OpenAI Connectors require OAuth authentication. You’ll need to register an OAuth client with the service provider first. See OpenAI’s documentation for additional instructions. Once you register your OAuth API client you can create and deploy the service just like we did for the MCP Server. Step 1: Create the Service YAML
kind: Service
version: v1
metadata:
  name: microsoft-teams-connector
  description: "Expose Microsoft Teams via OpenAI Connector"
ports:
- num: "*"
  type: http
  endpoints:
  - path: /
    mcp:
      label: "microsoft-teams-connector"
      description: "OpenAI Connector for Microsoft Teams"
      openai_connector_id: "connector_microsoftteams"
      oauth:
        client_id: "YOUR_CLIENT_ID"
        client_secret: "..."
        authorization_endpoint: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
        token_endpoint: "https://login.microsoftonline.com/common/oauth2/v2.0/token"
        no_pkce: false                    # Optional: depends on if pkce is used
        scopes: ["User.Read", "Chat.Read", "ChannelMessage.Read.All"]
Step 2: Deploy the Service
meshagent service create --file="meshagent.yaml"  # optional: --room=myroom
Step 3: Start the CLI ChatBot with MCP Enabled
meshagent chatbot join --room=myroom --agent-name=agent --mcp
# make sure room matches room you deployed to if deployed as a room service
Step 4: Use It in MeshAgent Studio Open MeshAgent Studio, go to your room, and you’ll see “Microsoft Teams” as a toggleable tool. Enable it to let the agent read Teams messages!