Metadata-Version: 2.5
Name: ory-langchain
Version: 1.8.1
Summary: Ory Agent Security for LangChain / LangGraph — per-tool authorization, activity logging, and identity propagation via an AgentMiddleware. Built on ory-argus.
Author: Ory
License-Expression: Apache-2.0
Keywords: agent,ai,authorization,langchain,langgraph,ory,permissions
Requires-Python: >=3.10
Requires-Dist: langchain<2,>=1
Requires-Dist: ory-argus<2,>=1
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.17.2; extra == 'dev'
Description-Content-Type: text/markdown

# ory-langchain

Ory Agent Security for [LangChain](https://docs.langchain.com) / LangGraph.

Adds three things to a LangChain agent with one line:

1. **Authorization** — every tool call is checked against Ory Permissions before it runs.
2. **Activity auditing** — every invocation is recorded in the unified privacy-safe NDJSON log.
3. **Identity propagation** — user → agent → sub-agent delegation, recorded in Ory.

## Install

```bash
pip install ory-langchain
```

## Use

```python
from langchain.agents import create_agent
from ory_langchain import OryMiddleware

agent = create_agent(
    model,
    tools=[search, send_email],
    middleware=[OryMiddleware()],   # gate every tool call through Ory
)
```

`OryMiddleware()` reads the shared Ory config at `~/.config/ory-agent-plugins/config.json`,
so a login performed by any Ory coding-agent harness — or by `ory-argus login` — is reused
transparently. Configure a project explicitly with `OryMiddleware(project_url=...)`.

### Permission mode

- `observe` (default) — denied tools still run, but a `permission.observe_deny` activity event is
  recorded. Use this to see what *would* be blocked before enforcing.
- `enforce` — denied tools are blocked: the model receives an error `ToolMessage` and the
  tool never executes. The posture is read from the Ory project — an admin promotes it to
  enforce in the Ory Console (Agent Security); `ory-argus permissions` shows what
  this machine resolves.

### MCP tools

Tools loaded with [`langchain-mcp-adapters`](https://github.com/langchain-ai/langchain-mcp-adapters)
are checked as `McpServer:<server>` and `McpTool:<server>:<tool>` when the MCP client carries
Ory's tool interceptor:

```python
from langchain_mcp_adapters.client import MultiServerMCPClient

connections = {
    "github": {"transport": "streamable_http", "url": "https://api.githubcopilot.com/mcp/"},
    "playwright": {"transport": "stdio", "command": "npx", "args": ["-y", "@playwright/mcp"]},
}
ory = OryMiddleware()
mcp = MultiServerMCPClient(connections, tool_interceptors=[ory.mcp_interceptor(connections=connections)])

agent = create_agent(model, tools=[search, *await mcp.get_tools()], middleware=[ory])
```

The interceptor reports the `connections` key as the server, the server's raw tool name
(without the `tool_name_prefix` prefix), and hashed fingerprints of the server's URL host or
launched package, so Agent Security matches the server to its catalog whatever you named it.
Only the hashes leave the process; URLs, arguments, and headers do not. In enforce mode a denied
call returns an MCP error result to the model and the server is never called.

`OryMiddleware` leaves tools whose MCP client carries an Ory interceptor to that interceptor, so
each MCP call is checked and recorded once. Without the interceptor, the middleware still gates
MCP tools, but as plain tools. `OryMcpInterceptor(client=..., connections=...)` also works on
its own, for MCP tools used outside an agent with the middleware.

### Activity only

If you only want lifecycle activity (no enforcement), use the callback handler instead:

```python
from ory_langchain import OryCallbackHandler

agent.invoke({"messages": [...]}, config={"callbacks": [OryCallbackHandler()]})
```

Built on [`ory-argus`](https://pypi.org/project/ory-argus/), the shared Ory Agent Security core.
