LangChain

Integrate BlueNexus MCP tools into your LangChain application.

Install

pip install langchain-mcp-adapters langchain-openai mcp

Setup

import asyncio
from langchain_mcp_adapters.tools import load_mcp_tools
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
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:
            await session.initialize()

            # Load BlueNexus tools as LangChain tools
            tools = await load_mcp_tools(session)

            # Create a LangChain agent with the tools
            model = ChatOpenAI(model="gpt-4o")
            agent = create_react_agent(model, tools)

            # Run the agent
            result = await agent.ainvoke({
                "messages": [
                    {"role": "user", "content": "What's on my Google Calendar today?"}
                ]
            })

            print(result["messages"][-1].content)

asyncio.run(main())

How It Works

  1. load_mcp_tools() converts BlueNexus's MCP tools (use-agent, list-connections) into LangChain-compatible tools
  2. The LangChain agent can call these tools like any other LangChain tool
  3. When the agent calls use-agent, it delegates to the BlueNexus ReAct agent, which handles the actual service interaction

With LangGraph

For more complex workflows:

from langgraph.graph import StateGraph, MessagesState

async def bluenexus_node(state: MessagesState):
    # Use the MCP session to call BlueNexus directly
    result = await session.call_tool(
        "use-agent",
        arguments={"prompt": state["messages"][-1].content}
    )
    return {"messages": [{"role": "assistant", "content": result.content[0].text}]}

graph = StateGraph(MessagesState)
graph.add_node("bluenexus", bluenexus_node)
# ... add edges and compile

Multi-Service Example

result = await agent.ainvoke({
    "messages": [
        {
            "role": "user",
            "content": "Get my upcoming meetings from Google Calendar "
                       "and post a summary to the #standup channel in Slack"
        }
    ]
})

The BlueNexus agent handles the cross-service coordination internally.

Next Steps