Custom MCP Integration

Connect to BlueNexus MCP programmatically using the official MCP SDK. This is the foundation for all framework integrations.

TypeScript

Install

npm install @modelcontextprotocol/sdk

Connect and Call Tools

import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";

const TOKEN = process.env.BLUENEXUS_TOKEN;

// Create transport (Streamable HTTP, not the deprecated SSE transport)
const transport = new StreamableHTTPClientTransport(
  new URL("https://api.bluenexus.ai/mcp"),
  {
    requestInit: {
      headers: {
        Authorization: `Bearer ${TOKEN}`,
      },
    },
  }
);

// Create client and connect
const client = new Client({
  name: "my-app",
  version: "1.0.0",
});

await client.connect(transport);

// List available tools
const { tools } = await client.listTools();
console.log("Available tools:", tools.map((t) => t.name));

// List user's connections
const connections = await client.callTool({
  name: "list-connections",
  arguments: {},
});
console.log("Connections:", connections.content);

// Call the agent
const result = await client.callTool({
  name: "use-agent",
  arguments: {
    prompt: "What's on my Google Calendar today?",
    connector: "google", // optional: hint which service to use
  },
});
console.log("Result:", result.content);

// Clean up
await client.close();

With OAuth (Full Flow)

If you need to implement the OAuth flow instead of using a PAT:

import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";

async function connectWithOAuth(accessToken: string) {
  const transport = new StreamableHTTPClientTransport(
    new URL("https://api.bluenexus.ai/mcp"),
    {
      requestInit: {
        headers: {
          Authorization: `Bearer ${accessToken}`,
        },
      },
    }
  );

  const client = new Client({
    name: "my-app",
    version: "1.0.0",
  });

  await client.connect(transport);
  return client;
}

// Use with a token obtained through the OAuth flow
const client = await connectWithOAuth(userAccessToken);

Python

Install

pip install mcp

Connect and Call Tools

import asyncio
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client

BLUENEXUS_TOKEN = "your-personal-access-token"

async def main():
    headers = {"Authorization": f"Bearer {BLUENEXUS_TOKEN}"}

    async with streamablehttp_client(
        "https://api.bluenexus.ai/mcp",
        headers=headers
    ) as (read_stream, write_stream, _):
        async with ClientSession(read_stream, write_stream) as session:
            # Initialize
            await session.initialize()

            # List tools
            tools = await session.list_tools()
            print("Tools:", [t.name for t in tools.tools])

            # List connections
            connections = await session.call_tool(
                "list-connections",
                arguments={}
            )
            print("Connections:", connections.content)

            # Call the agent
            result = await session.call_tool(
                "use-agent",
                arguments={
                    "prompt": "What's on my Google Calendar today?",
                    "connector": "google"
                }
            )
            print("Result:", result.content)

asyncio.run(main())

Error Handling

try {
  const result = await client.callTool({
    name: "use-agent",
    arguments: { prompt: "..." },
  });

  if (result.isError) {
    console.error("Tool error:", result.content);
  } else {
    console.log("Success:", result.content);
  }
} catch (error) {
  // HTTP-level errors (401, 402, 429)
  console.error("Request failed:", error);
}

Important Notes

  • Use StreamableHTTPClientTransport, not the deprecated SSEClientTransport
  • The MCP endpoint is rate-limited to 30 requests/minute
  • Long-running use-agent calls benefit from SSE streaming for progress updates
  • Each use-agent call consumes credits based on LLM token usage

Next Steps