> ## Documentation Index
> Fetch the complete documentation index at: https://www.truefoundry.com/llms.txt
> Use this file to discover all available pages before exploring further.

# MCP Gateway

> Learn how to use the MCP Gateway SDK to securely connect to integrated MCP Servers through TrueFoundry AI Gateway.

## Overview

TrueFoundry AI Gateway enables you to expose integrated MCP Servers to developers in a secure manner.

<Frame>
  <img src="https://mintcdn.com/truefoundry/5ZMNUF311tgGO_yT/images/mcp-gateway.png?fit=max&auto=format&n=5ZMNUF311tgGO_yT&q=85&s=9be8cdd5a76126e2c196bbf9c4034ee2" alt="Diagram showing TrueFoundry AI Gateway architecture with MCP Servers integration" width="3795" height="1518" data-path="images/mcp-gateway.png" />
</Frame>

This allows you to:

* Manage who can use the registered MCP Servers.
* Avoid exposing MCP Server credentials directly to the developers.
* Monitor usage metrics (latency, execution per second) of the tools across all registered MCP Servers. (Coming soon.)
* Rate-limit tool calls. (Coming soon.)
  * This is useful for expensive tools like search, code-execution, etc.

***

## Quickstart

<Steps>
  <Step title="Copy the code snippet">
    <Frame caption="Get your code snippet from the AI Gateway UI">
      <img src="https://mintcdn.com/truefoundry/n3EuZuJ0K8wBFp1G/images/mcp-server-gateway-proxy-code-snippet.png?fit=max&auto=format&n=n3EuZuJ0K8wBFp1G&q=85&s=a2429abe7adc535c6537a2651715525b" alt="TrueFoundry Dashboard showing where to find the MCP server code snippet in AI Gateway UI" width="3590" height="1668" data-path="images/mcp-server-gateway-proxy-code-snippet.png" />
    </Frame>

    <Frame caption="Copy the code snippet from the UI for quick integration">
      <img src="https://mintcdn.com/truefoundry/5ZMNUF311tgGO_yT/images/mcp-server-gateway-proxy-code-snippet-ui.png?fit=max&auto=format&n=5ZMNUF311tgGO_yT&q=85&s=5e9f4bafa229a9e29b8a462dd17b6b02" alt="TrueFoundry Dashboard showing code snippet interface for MCP server integration" width="3594" height="1480" data-path="images/mcp-server-gateway-proxy-code-snippet-ui.png" />
    </Frame>
  </Step>

  <Step title="Copy PAT or VAT">
    See [API Keys](/docs/generating-truefoundry-api-keys) for more details on creating PATs and VATs.
  </Step>

  <Step title="Call the MCP Server from your code">
    At this point, we can use the code snippet we copied at Step 1.

    Below is an example showing how to connect, list tools, and call tools on an MCP Server. Update the placeholders with your actual credentials and tool names.

    <Tabs>
      <Tab title="TypeScript">
        Install the library:

        ```bash lines theme={"dark"}
        npm install @modelcontextprotocol/sdk
        ```

        Code:

        ```typescript lines theme={"dark"}
        import { Client } from "@modelcontextprotocol/sdk/client/index.js";
        import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";

        // Example usage (runs if this file is executed directly)
        // Run with: npx ts-node typescript-mcp-client.ts
        (async () => {
            const url = '{GATEWAY_BASE_URL}/mcp/<integrationId>/server';
            const authToken = '<tfy-api-token>'; // Replace with your actual TFY API token (PAT or Service account token)
            const toolName = '<tool-name>';      // Replace with your actual tool name
            const args = {};                     // Replace with your actual tool arguments

            const requestInit: RequestInit = {
                headers: { Authorization: `Bearer ${authToken}` }
            };
            const baseUrl = new URL(url);
            const client = new Client({ name: "streamable-http-client", version: "1.0.0" });
            await client.connect(new StreamableHTTPClientTransport(baseUrl, { requestInit }));

            const tools = await client.listTools();
            console.log("Available tools:", tools);

            const result = await client.callTool({ name: toolName, arguments: args });
            console.log("Tool result:", result);
        })();

        ```
      </Tab>

      <Tab title="Python">
        Install the library:

        ```bash lines theme={"dark"}
        pip install fastmcp
        ```

        Code:

        ```python lines theme={"dark"}
        from fastmcp import Client

        token = "<tfy-api-token>" # Replace with your actual TFY API token (PAT or VA Token)
        async def main():
            url = "{GATEWAY_BASE_URL}/mcp/<integrationId>/server"
            async with Client(url, auth=token) as client:
                tools = await client.list_tools()
                for tool in tools:
                    print(f"Tool: {tool.name}")
                    print(f"Description: {tool.description}")
                    print(f"Parameters: {tool.inputSchema}")
                print("--------------------------------")
                tool_name = "echo"  # Replace with your actual tool name
                tool_args = {"message": "hello"}  # Replace with your actual tool arguments
                results = await client.call_tool(tool_name, tool_args)
                print(f"Result: {results}")
                print("--------------------------------")

        if __name__ == "__main__":
            import asyncio
            asyncio.run(main())
        ```
      </Tab>
    </Tabs>

    ## Configuration

    <Card title="Required Parameters">
      | Parameter   | Description                                                                |
      | ----------- | -------------------------------------------------------------------------- |
      | `url`       | The MCP server URL (e.g., `{GATEWAY_BASE_URL}/mcp/<integrationId>/server`) |
      | `authToken` | Your TrueFoundry API token (PAT or user token)                             |
      | `toolName`  | The name of the tool you want to invoke                                    |
      | `args`      | The arguments to pass to the tool                                          |
    </Card>

    <Note>
      Replace the placeholders in the example:

      * `{GATEWAY_BASE_URL}` with your TrueFoundry AI Gateway base URL
      * `<integrationId>` with your MCP server ID
      * `<tfy-api-token>` with your TrueFoundry API token
      * `<tool-name>` with the name of the tool you want to use
    </Note>
  </Step>
</Steps>

## Supported Transports

* Only MCP servers that use [**streamable-http transport**](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http) (stateful/stateless) are supported for connection via Gateway proxy
* Older SSE-based MCP servers will work on `/agent/responses` endpoint but are not compatible with the proxy connection method described here
