Developer documentation

LangChain (Python)

Call paid MCP tools through Nexus402 from LangChain (Python): one URL, one header, and the framework's own MCP client. Nothing of ours to install.

Sections

LangChain is a Python framework for building agents from models and tools. With langchain-mcp-adapters, every Nexus402 tool your key may call becomes a LangChain tool your agent can choose, with its schema and description intact.

The example connects to https://api.nexus402.dev/v1/mcp over streamable HTTP, sends your key in the X-Nexus-Api-Key header, lists the tools the key may call, and calls the one you name in NEXUS402_TOOL. A call to a paid tool is charged to your wallet. If you name none, nothing is called. It is run against a test gateway at langchain-mcp-adapters 0.3.2, so a release that changes how headers are passed is caught before you meet it.

Install and run #

Python. Put the header in the connection's headers; every tool the key may call becomes a LangChain tool.

pip install -r requirements.txt
export NEXUS402_API_KEY=nx_dev_...
# The tool to call, by its name in the marketplace. A paid tool is charged.
export NEXUS402_TOOL='<tool name>'
python main.py

The example #

"""Call a Nexus402 tool through LangChain's MCP adapters (ADR-0236).

The adapters turn every tool the key may call into a LangChain tool, so an
agent built with LangChain or LangGraph can use them directly. Here one is
invoked by hand: the one NEXUS402_TOOL names (required: a call is charged).
The key comes from NEXUS402_API_KEY, never from code.
"""

import asyncio
import json
import os
import sys

from langchain_mcp_adapters.client import MultiServerMCPClient

URL = os.environ.get("NEXUS402_MCP_URL", "https://api.nexus402.dev/v1/mcp")
TOOL = os.environ.get("NEXUS402_TOOL", "")


def routing_name(tool) -> str:
    """The tool's routing name (acme/weather/forecast), when the list names it
    by its MCP-safe listed name (acme_weather_forecast, ADR-0264) and the
    adapters keep the row's _meta. So NEXUS402_TOOL may be either spelling."""
    meta = (getattr(tool, "metadata", None) or {}).get("_meta")
    return meta.get("nexus402/name", "") if isinstance(meta, dict) else ""


async def main() -> int:
    client = MultiServerMCPClient(
        {
            "nexus402": {
                "transport": "streamable_http",
                "url": URL,
                "headers": {"X-Nexus-Api-Key": os.environ["NEXUS402_API_KEY"]},
            }
        },
        handle_tool_errors=False,
    )
    tools = await client.get_tools()
    print(f"{len(tools)} tools listed")
    tool = next(t for t in tools if TOOL in (t.name, routing_name(t)))
    args = json.loads(os.environ.get("NEXUS402_ARGS", "{}"))
    try:
        result = await tool.ainvoke(args)
    except Exception as error:  # a 402 or another refusal
        print(f"call refused: {error}")
        return 2
    print(result)
    return 0


if __name__ == "__main__":
    # Never a default: a call is charged, and you choose who is paid.
    if not TOOL:
        sys.exit(
            "Set NEXUS402_TOOL to the name of the tool to call. Nothing was called. "
            "A call to a paid tool is charged to your wallet."
        )
    sys.exit(asyncio.run(main()))

When a call is refused #

A call your wallet or your key's cap cannot cover is refused with HTTP 402 before anything is charged, and the example prints call refused and exits with code 2. A call that fails before any output reaches your agent is not charged either. Every refusal code is in the error reference.

Next #

Install agent tools

Help your coding agent stay on track when building with Nexus402. Learn more

Paste this into Claude Code, Codex or Cursor. Your agent connects itself to Nexus402 and adds the skills, for whichever tool it is.

Prompt for your agent text
Set up Nexus402 for me in this coding agent.
1. Read https://www.nexus402.dev/docs/agents.md and work out which tool you are (Claude Code, Codex or Cursor).
2. Add the Nexus402 MCP server https://api.nexus402.dev/v1/mcp the way that page shows for your tool, sending the X-Nexus-Api-Key header from the NEXUS402_API_KEY environment variable.
3. Install the skills listed at https://www.nexus402.dev/.well-known/skills/index.json where your tool keeps skills or rules.
Never write my key into a file, a commit or a reply. If NEXUS402_API_KEY is not set, stop and tell me to set it.
Then check the connection lists tools, and tell me what you changed.

Connect the gateway, then add the two skills.

Terminal bash
claude mcp add --transport http --scope user nexus402 https://api.nexus402.dev/v1/mcp \
  --header "X-Nexus-Api-Key: $NEXUS402_API_KEY"

mkdir -p ~/.claude/skills/nexus402-tools && curl -fsSL https://www.nexus402.dev/.well-known/skills/nexus402-tools/SKILL.md -o ~/.claude/skills/nexus402-tools/SKILL.md
mkdir -p ~/.claude/skills/nexus402-endpoint && curl -fsSL https://www.nexus402.dev/.well-known/skills/nexus402-endpoint/SKILL.md -o ~/.claude/skills/nexus402-endpoint/SKILL.md

Add the gateway to ~/.codex/config.toml, then add the skills with npx skills add https://www.nexus402.dev.

~/.codex/config.toml toml
[mcp_servers.nexus402]
url = "https://api.nexus402.dev/v1/mcp"
env_http_headers = { "X-Nexus-Api-Key" = "NEXUS402_API_KEY" }
tool_timeout_sec = 130
Add to Cursor

Opens Cursor and asks to add the gateway. Your key is read from NEXUS402_API_KEY.

Or add it by hand to ~/.cursor/mcp.json. Then add the skills with npx skills add https://www.nexus402.dev.

~/.cursor/mcp.json json
{
  "mcpServers": {
    "nexus402": {
      "url": "https://api.nexus402.dev/v1/mcp",
      "headers": {
        "X-Nexus-Api-Key": "${env:NEXUS402_API_KEY}"
      }
    }
  }
}

Skills-only installs don't update automatically.

Terminal bash
npx skills add https://www.nexus402.dev

Every option reads your key from NEXUS402_API_KEY; none writes it into a file. Learn more