# LocalContextMiddleware

> **Class** in `deepagents_code`

📖 [View in docs](https://reference.langchain.com/python/deepagents-code/local_context/LocalContextMiddleware)

Inject local context (git state, project structure, etc.) into the system prompt.

Runs a bash detection script via `backend.execute()` on first interaction
and stores that snapshot for stable system-prompt injection. After each
summarization event, changed context is appended as an internal conversation
message so the cached prompt prefix stays byte-identical.

Because the script runs inside the backend, it works for both local shells
and remote sandboxes.

## Signature

```python
LocalContextMiddleware(
    self,
    backend: _ExecutableBackend | _AsyncExecutableBackend,
    *,
    mcp_server_info: list[MCPServerInfo] | None = None,
    tracing_project: str | None = None,
    user_tracing_project: str | None = None,
)
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `backend` | `_ExecutableBackend \| _AsyncExecutableBackend` | Yes | Backend instance that provides shell command execution. |
| `mcp_server_info` | `list[MCPServerInfo] \| None` | No | MCP server metadata to include in the system prompt. (default: `None`) |
| `tracing_project` | `str \| None` | No | LangSmith project the agent's own runs trace to, or `None` when tracing is disabled (the tracing section is omitted). (default: `None`) |
| `user_tracing_project` | `str \| None` | No | User's original `LANGSMITH_PROJECT` used by shell commands the agent runs. (default: `None`) |

## Extends

- `AgentMiddleware`

## Constructors

```python
__init__(
    self,
    backend: _ExecutableBackend | _AsyncExecutableBackend,
    *,
    mcp_server_info: list[MCPServerInfo] | None = None,
    tracing_project: str | None = None,
    user_tracing_project: str | None = None,
) -> None
```

| Name | Type |
|------|------|
| `backend` | `_ExecutableBackend \| _AsyncExecutableBackend` |
| `mcp_server_info` | `list[MCPServerInfo] \| None` |
| `tracing_project` | `str \| None` |
| `user_tracing_project` | `str \| None` |


## Properties

- `trace_policy`
- `state_schema`
- `backend`

## Methods

- [`before_agent()`](https://reference.langchain.com/python/deepagents-code/local_context/LocalContextMiddleware/before_agent)
- [`abefore_agent()`](https://reference.langchain.com/python/deepagents-code/local_context/LocalContextMiddleware/abefore_agent)
- [`wrap_model_call()`](https://reference.langchain.com/python/deepagents-code/local_context/LocalContextMiddleware/wrap_model_call)
- [`awrap_model_call()`](https://reference.langchain.com/python/deepagents-code/local_context/LocalContextMiddleware/awrap_model_call)

---

[View source on GitHub](https://github.com/langchain-ai/deepagents/blob/3812967c84d90619848f3185f22470204fc88bd8/libs/code/deepagents_code/local_context.py#L698)