Skip to content

feat(langchain): add Stagehand code-mode MCP example - #2629

Open
shrey150 wants to merge 23 commits into
shrey/stg-2765-codemode-crewaifrom
shrey/stg-2765-codemode-langchain
Open

feat(langchain): add Stagehand code-mode MCP example#2629
shrey150 wants to merge 23 commits into
shrey/stg-2765-codemode-crewaifrom
shrey/stg-2765-codemode-langchain

Conversation

@shrey150

@shrey150 shrey150 commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Why

Stagehand code mode executes model-generated JavaScript. This integration keeps that execution behind the package-installed Vercel Sandbox boundary from #2626 while preserving the explicit stateful MCP session LangChain requires.

Stack

  1. #2644 — publishable @browserbasehq/stagehand-codemode package and CLI
  2. #2626 — authenticated, package-installed Vercel Sandbox boundary
  3. #2627 — Mastra integration
  4. #2628 — CrewAI integration
  5. This PR — LangChain / Deep Agents integration
  6. Next — Eve integration layered on this branch

What changed

  • replaces the former Modal/stdin bridge with MultiServerMCPClient over authenticated Streamable HTTP
  • starts the reviewed TypeScript Vercel Sandbox lease and passes LangChain only its { url, token } connection
  • holds one explicit client.session("stagehand") across discovery, direct calls, and the complete Deep Agent invocation; the default convenience path is intentionally not used because it is stateless
  • discovers the package's canonical code_execute description and uses it as the Deep Agent system prompt instead of copying the schema, executor, or skill
  • forwards only an explicit Browserbase/Vercel/artifact/runtime allowlist to the lease process; model keys and host-only markers stay outside
  • pins the exact tested dependency set: Deep Agents 0.7.5, LangChain MCP adapters 0.3.2, LangChain OpenAI 1.4.2, and MCP SDK 1.29.0

Proof matrix

Proof Result What it establishes
Python 3.12 compile + Ruff Passed The facade, adapter, unit suite, and live proof are syntactically valid and formatted.
Shared lease unit suite 3/3 passed Model credentials are excluded, stdin closes before process wait, and cleanup failures preserve the primary error.
LangChain adapter unit suite 4/4 passed Bearer-authenticated Streamable HTTP config, exact tool discovery, canonical guidance validation, and model tool-call accounting.
Example-directory import Passed The documented standalone sandbox and agent imports resolve.
Missing-config execution Failed closed in 3.7s The real lease subprocess reports the missing artifact input and exits without hanging.
pnpm check 9/9 tasks passed across 11 workspaces Workspace formatting, lint, generated-content checks, and TypeScript types remain valid.
Exact code-mode build + sandbox artifact pack Passed The sandbox input contains tarballs built from the exact Stagehand and code-mode commits under review, with a verified manifest and lockfile.
Live Deep Agents workflow Wired into Actions It requires three direct calls on one page plus a real openai:gpt-5-mini model-selected call, independent DOM verification, credential isolation, and ordered cleanup before emitting PASS.

The live job intentionally fails closed when repository Browserbase, Vercel, or OpenAI credentials are absent. Repository Vercel credentials are not currently configured for this workflow, so no live pass is claimed yet.

Changeset

None. This adds an integration example and CI proof surface without changing a published API.

@changeset-bot

changeset-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: c91a195

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@socket-security

socket-security Bot commented Aug 6, 2026

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedpypi/​deepagents@​0.7.598100100100100
Addedpypi/​mcp@​1.29.099100100100100
Addedpypi/​langchain-openai@​1.4.2100100100100100
Addedpypi/​langchain-mcp-adapters@​0.3.2100100100100100

View full report

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread packages/integrations/examples/langchain/agent.py Outdated
Comment thread packages/integrations/examples/langchain/agent.py Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 8 files (changes from recent commits).

Confidence score: 5/5

  • In packages/integrations/examples/langchain/agent.py, the new build_stagehand_agent, run_stagehand_agent, and stagehand_code_session paths are not covered by focused unit tests, so behavior regressions in agent construction or session scoping could slip through unnoticed; add targeted tests for these flows (including success and failure/session-boundary cases) to de-risk future changes.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="packages/integrations/examples/langchain/agent.py">

<violation number="1" location="packages/integrations/examples/langchain/agent.py:63">
P3: The newly added agent-construction and session-scoping paths (`build_stagehand_agent`, `run_stagehand_agent`, `stagehand_code_session`) have no focused unit tests — the existing tests cover only the client shape, tool-description validation, and the tool-call counter. These paths are currently validated only through the live, model-dependent e2e. Adding focused tests that verify `build_stagehand_agent` passes the tool description as the system prompt, rejects non-`code_execute` tools, and that `stagehand_code_session` scopes the loaded tool to one session would encode the contract and protect against regressions independently of the flaky real-model run.</violation>
</file>

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread packages/integrations/examples/langchain/README.md
Comment thread .github/workflows/codemode-framework-examples.yml Outdated
yield await load_stagehand_code_tool(session)


def build_stagehand_agent(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The newly added agent-construction and session-scoping paths (build_stagehand_agent, run_stagehand_agent, stagehand_code_session) have no focused unit tests — the existing tests cover only the client shape, tool-description validation, and the tool-call counter. These paths are currently validated only through the live, model-dependent e2e. Adding focused tests that verify build_stagehand_agent passes the tool description as the system prompt, rejects non-code_execute tools, and that stagehand_code_session scopes the loaded tool to one session would encode the contract and protect against regressions independently of the flaky real-model run.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/integrations/examples/langchain/agent.py, line 63:

<comment>The newly added agent-construction and session-scoping paths (`build_stagehand_agent`, `run_stagehand_agent`, `stagehand_code_session`) have no focused unit tests — the existing tests cover only the client shape, tool-description validation, and the tool-call counter. These paths are currently validated only through the live, model-dependent e2e. Adding focused tests that verify `build_stagehand_agent` passes the tool description as the system prompt, rejects non-`code_execute` tools, and that `stagehand_code_session` scopes the loaded tool to one session would encode the contract and protect against regressions independently of the flaky real-model run.</comment>

<file context>
@@ -3,120 +3,105 @@
+        yield await load_stagehand_code_tool(session)
+
+
+def build_stagehand_agent(
+    code_tool: BaseTool,
+    model: str | Any = DEFAULT_STAGEHAND_MODEL,
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in c29ce4a8. The focused suite now verifies canonical tool-description → system-prompt wiring, rejects non-code_execute tools without reflecting remote names, proves one named MCP session owns the loaded tool, and exercises run_stagehand_agent input/config wiring. The exact pinned environment passes 9/9 agent/session tests.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 1 file (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread .github/workflows/codemode-framework-examples.yml

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 7 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread packages/integrations/examples/langchain/agent.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant