Overview
You can use the official Python/TypeScript MCP SDK to connect to MCP Servers that have been added to the TrueFoundry Gateway. The Gateway handles authorization for those servers - you only need to provide authentication for the Gateway itself.
Transport Support:
- Only MCP servers that use streamable-http transport (stateful/stateless) are supported for connection via Gateway proxy
- SSE-based MCP servers will work on
/agent/responses
endpoint but are not compatible with the proxy connection method described here
Get your code snippet from the AI Gateway UI
Copy the code snippet from the UI for quick integration
Installation
npm install @modelcontextprotocol/sdk
npm install @modelcontextprotocol/sdk
Usage
Below are complete examples of how to use the SDK to connect, list tools, and call tools on an MCP Server. Just copy the code, update your credentials and tool names, and you’re ready to go!
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 = 'https://<controlPlaneUrl>/api/llm/mcp-server/<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);
})();
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 = 'https://<controlPlaneUrl>/api/llm/mcp-server/<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);
})();
from fastmcp import Client
token = "<tfy-api-token>" # Replace with your actual TFY API token (PAT or VA Token)
async def main():
url = "https://<controlPlaneUrl>/api/llm/mcp-server/<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())
Configuration
Required Parameters
Parameter | Description |
---|
url | The MCP server URL (e.g., https://<controlPlaneUrl>/api/llm/mcp-server/<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 |
Replace the placeholders in the example:
<controlPlaneUrl>
with your TrueFoundry control plane URL
<integrationId>
with your MCP server integration ID
<tfy-api-token>
with your TrueFoundry API token
<tool-name>
with the name of the tool you want to use
Tips
- Make sure your API token has the necessary permissions for the MCP server and tools you want to access.