Skip to main content

threadplane-middleware

LangGraph middleware for binding client-declared tool stubs and routing client tool calls to END so the browser executes them.

This is the Python LangGraph package. For LangGraph.js, use @threadplane/middleware from npm and import @threadplane/middleware/langgraph.

How it works

When a browser client sends a tool catalog ({name, description, parameters} dicts) along with a run request, the graph can expose those tools to the LLM and route their calls back to the browser instead of executing them server-side. The browser then executes the call and re-runs the graph with a ToolMessage carrying the result.

The catalog is read from state["tools"], falling back to state["client_tools"] if tools is absent.

Installation

pip install threadplane-middleware

Usage

from langgraph.graph import END, StateGraph
from threadplane.middleware.langgraph import bind_client_tools, route_after_agent

# Server-side tools your graph owns
SERVER_TOOLS = [search_tool, calculator_tool]
base_llm = ChatOpenAI(model="gpt-4o")

def agent_node(state):
    # bind_client_tools must be called per-run inside the node because
    # the client catalog arrives in state and may differ between runs.
    llm = bind_client_tools(base_llm, SERVER_TOOLS, state)
    response = llm.invoke(state["messages"])
    return {"messages": [response]}

def router(state):
    # Returns "tools" for server tool calls, "__end__" otherwise.
    # Map "__end__" to LangGraph's END in add_conditional_edges.
    return route_after_agent(state, [t.name for t in SERVER_TOOLS])

graph = StateGraph(...)
graph.add_node("agent", agent_node)
graph.add_node("tools", ToolNode(SERVER_TOOLS))
graph.add_conditional_edges("agent", router, {"tools": "tools", "__end__": END})

What happens with a client tool call

  1. The LLM emits a tool call whose name matches a client-declared tool.
  2. route_after_agent returns "__end__" — the graph run ends.
  3. The browser receives the partial output, executes the tool locally, and re-runs the graph with a ToolMessage containing the result.
  4. The LLM continues from there as if it had called a server tool.

Lower-level helpers

from threadplane.middleware.langgraph import (
    client_tool_specs,   # → list of OpenAI function-tool dicts
    client_tool_names,   # → set[str] of client tool names
    has_client_tool_call,  # → bool
    has_server_tool_call,  # → bool
    last_message,          # → last message from state["messages"]
)

Development

uv venv
uv run --extra test python -m pytest -q

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

threadplane_middleware-0.0.2.tar.gz (97.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

threadplane_middleware-0.0.2-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file threadplane_middleware-0.0.2.tar.gz.

File metadata

  • Download URL: threadplane_middleware-0.0.2.tar.gz
  • Upload date:
  • Size: 97.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for threadplane_middleware-0.0.2.tar.gz
Algorithm Hash digest
SHA256 964925aef5cc5ed8a9a9bb1c7ea7f740ee010291b6ad0bec35f50ac3f41d9809
MD5 942b2fd48ebd5f62d06f15b8ab7b3463
BLAKE2b-256 77f64f5bce0536c584677f191223821c74d0617bde36d45640008e8adf123c79

See more details on using hashes here.

File details

Details for the file threadplane_middleware-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: threadplane_middleware-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.7 {"installer":{"name":"uv","version":"0.12.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for threadplane_middleware-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3bf42291b06b49f6e59eb92672fa88d9f14467a5222fec806531714d4349f17b
MD5 8144b7a4c36dd6909462b7e3bb84831f
BLAKE2b-256 96a40bc72be56ee222a15e151a8eb99decb749acf44c964ef12c240ec6a8f76f

See more details on using hashes here.

Release history Release notifications | RSS feed

0.0.3

2 files

This release

0.0.2 This release

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page