feat(langchain): add Stagehand code-mode MCP example - #2629
Conversation
|
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
There was a problem hiding this comment.
2 issues found and verified against the latest diff
Confidence score: 3/5
- In
packages/integrations/examples/langchain/agent.py, the custom child-processenvhandling appears to replace the full environment instead of layering overrides, which can break MCP startup or drop inherited credentials when callers pass partial values — merge overrides intoos.environbefore spawning the child. - In
packages/integrations/examples/langchain/agent.py,STAGEHAND_CODEMODE_SKILLis loaded fromSKILL.mdat import time, sosmoke.pypulls in file I/O and possible missing-file failures even when the skill text is unused — defer this read until runtime (or guard it) so smoke imports stay lightweight and reliable.
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:20">
P3: `STAGEHAND_CODEMODE_SKILL` reads SKILL.md at module import time. Since `smoke.py` imports from `agent.py` but never needs the skill string, that file read also runs whenever the smoke is imported, and a missing/misplaced SKILL.md would fail both scripts during import instead of when the agent actually runs. Consider resolving the skill text lazily inside `run_stagehand_agent` (or a small loader) so importing the helpers doesn't perform file I/O or raise.</violation>
<violation number="2" location="packages/integrations/examples/langchain/agent.py:33">
P2: Custom `env` values currently replace the full child environment, which can make the MCP child fail to start or lose credentials when only partial overrides are passed. Merging overrides into `os.environ` keeps default runtime variables while still allowing explicit overrides.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| "pnpm exec turbo run build --filter @browserbasehq/stagehand-integrations" | ||
| ) | ||
|
|
||
| child_env = dict(os.environ if env is None else env) |
There was a problem hiding this comment.
P2: Custom env values currently replace the full child environment, which can make the MCP child fail to start or lose credentials when only partial overrides are passed. Merging overrides into os.environ keeps default runtime variables while still allowing explicit overrides.
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 33:
<comment>Custom `env` values currently replace the full child environment, which can make the MCP child fail to start or lose credentials when only partial overrides are passed. Merging overrides into `os.environ` keeps default runtime variables while still allowing explicit overrides.</comment>
<file context>
@@ -0,0 +1,97 @@
+ "pnpm exec turbo run build --filter @browserbasehq/stagehand-integrations"
+ )
+
+ child_env = dict(os.environ if env is None else env)
+ return MultiServerMCPClient(
+ {
</file context>
| REPOSITORY_ROOT / "packages/integrations/dist/codemode/stdio-server.mjs" | ||
| ) | ||
| SKILL_PATH = REPOSITORY_ROOT / "packages/integrations/codemode/SKILL.md" | ||
| STAGEHAND_CODEMODE_SKILL = SKILL_PATH.read_text(encoding="utf-8").strip() |
There was a problem hiding this comment.
P3: STAGEHAND_CODEMODE_SKILL reads SKILL.md at module import time. Since smoke.py imports from agent.py but never needs the skill string, that file read also runs whenever the smoke is imported, and a missing/misplaced SKILL.md would fail both scripts during import instead of when the agent actually runs. Consider resolving the skill text lazily inside run_stagehand_agent (or a small loader) so importing the helpers doesn't perform file I/O or raise.
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 20:
<comment>`STAGEHAND_CODEMODE_SKILL` reads SKILL.md at module import time. Since `smoke.py` imports from `agent.py` but never needs the skill string, that file read also runs whenever the smoke is imported, and a missing/misplaced SKILL.md would fail both scripts during import instead of when the agent actually runs. Consider resolving the skill text lazily inside `run_stagehand_agent` (or a small loader) so importing the helpers doesn't perform file I/O or raise.</comment>
<file context>
@@ -0,0 +1,97 @@
+ REPOSITORY_ROOT / "packages/integrations/dist/codemode/stdio-server.mjs"
+)
+SKILL_PATH = REPOSITORY_ROOT / "packages/integrations/codemode/SKILL.md"
+STAGEHAND_CODEMODE_SKILL = SKILL_PATH.read_text(encoding="utf-8").strip()
+
+
</file context>
Why
The code-mode MCP should be proven through framework-native clients before the package is published. This layer ports the LangChain Deep Agents draft beside the canonical Stagehand implementation so it can test the exact local build without waiting for package publication.
Stack
code_execute, executor, configuration, and runtime testsWhat changed
MultiServerMCPClientclient.session("stagehand")open across discovery and the complete agent invocationcode_executeand supplies the canonical Stagehand skill as the system promptThe explicit session is the important lifecycle choice: LangChain's convenience
get_tools()path can create fresh stdio sessions per call, which would also create fresh browsers and lose prior page state.E2E Test Matrix
dist/codemode/stdio-server.mjs.ruff format --checkandruff checkover the Python exampleSTAGEHAND_BROWSER=localcode_executeran twice in one explicit session; page identity, title, and DOM marker persisted.STAGEHAND_BROWSER=browserbasecode_executeran twice against the same remote page and retained its DOM marker and title.code_executecalls, retained the same remote page and marker, and returnedDEEP_AGENT_PERSISTENCE_PASS.Changeset
None. This adds a private example and test surface without changing a published package.
Summary by cubic
Adds a LangChain Deep Agents example that runs the Stagehand code-mode MCP via stdio and keeps one explicit session so
code_executecalls share the same browser state. Includes a Python smoke test and a CI job that verifies persistence with a local browser.New Features
langchainDeep Agents example that launches the compiled stdio server and exposes onlycode_execute.client.session("stagehand")across discovery and the full agent run to preserve browser state.STAGEHAND_BROWSER,BROWSERBASE_API_KEY, optionalBROWSERBASE_PROJECT_ID).SKILL.mdas the agent system prompt.Dependencies
deepagents,langchain-mcp-adapters,langchain-openai,mcp.Written for commit d57bce1. Summary will update on new commits.