Source code for langgoap.integrations.langgraph_deploy

"""Scaffolding for deploying a LangGOAP graph via ``langgraph dev`` /
``langgraph deploy``.

When a LangGraph deployment is brought up (``langgraph dev <dir>`` for
local, ``langgraph deploy`` for LangGraph Platform), ``/mcp`` routes
are enabled by default — the deployment is automatically callable as
an MCP server.  See the ``disable_mcp`` field in
the langgraph-cli configuration schema
(https://github.com/langchain-ai/langgraph/blob/main/libs/cli/schemas/schema.json),
which defaults to ``False``.

This module provides :func:`scaffold_deployment`, a thin code-generator
that emits a complete deployable directory for a user-supplied
:class:`~langgoap.graph.builder.GoapGraph` factory.  No parallel MCP
server lives in LangGOAP — the LangGraph deployment surface owns that
contract.

Output directory shape::

    <out_dir>/
        langgraph.json     # langgraph-cli deployment config
        graph.py           # entrypoint that imports the user factory
                           # and binds ``graph = factory().compile()``
        .env.example       # OPENAI_API_KEY etc., users copy to .env
        README.md          # three-step run instructions
"""

from __future__ import annotations

import json
from pathlib import Path

# "." ships the scaffold directory itself (graph.py and any factory
# package placed beside it); without it a LangGraph Platform build has
# nothing to import the user's factory from (#65).
DEFAULT_DEPENDENCIES: tuple[str, ...] = (".", "langgoap")


[docs] def scaffold_deployment( out_dir: Path | str, *, graph_factory_module: str, graph_factory_attr: str, agent_name: str, agent_description: str, python_version: str = "3.12", extra_dependencies: tuple[str, ...] = (), force: bool = False, ) -> Path: """Emit a deployable LangGraph directory for a LangGOAP agent. Args: out_dir: Target directory. Created if missing. graph_factory_module: Importable module path the user holds their :class:`GoapGraph` factory in (e.g. ``"my_agent.graph"``). graph_factory_attr: Name of the no-argument callable inside ``graph_factory_module`` that returns a configured :class:`GoapGraph` (e.g. ``"build_graph"``). agent_name: Short identifier — becomes the key under ``graphs`` in ``langgraph.json`` and the MCP tool name once deployed. agent_description: Human-readable description, surfaced in the generated README. When LangGraph's MCP route enriches tool descriptions in a future release, this is also the natural place to source from. python_version: Python version pinned in ``langgraph.json``. Defaults to ``"3.12"`` — LangGraph requires ≥3.11. extra_dependencies: Extra pip dependencies appended to the default ``["langgoap"]``. Use to add LangChain provider packages (``"langchain-openai>=0.3"``), checkpointer backends, etc. force: When ``True``, overwrite existing files in ``out_dir``. Returns: The output directory path. Raises: FileExistsError: When ``out_dir`` already contains scaffolded files and ``force`` is ``False``. """ out = Path(out_dir) out.mkdir(parents=True, exist_ok=True) files = { "langgraph.json": _render_langgraph_json( agent_name=agent_name, python_version=python_version, extra_dependencies=extra_dependencies, ), "graph.py": _render_graph_py( graph_factory_module=graph_factory_module, graph_factory_attr=graph_factory_attr, ), ".env.example": _render_env_example(), "README.md": _render_readme( agent_name=agent_name, agent_description=agent_description, ), } for name, content in files.items(): path = out / name if path.exists() and not force: raise FileExistsError( f"{path} already exists; pass force=True to overwrite" ) path.write_text(content, encoding="utf-8") return out
# --------------------------------------------------------------------------- # File renderers # --------------------------------------------------------------------------- def _render_langgraph_json( *, agent_name: str, python_version: str, extra_dependencies: tuple[str, ...], ) -> str: deps = list(DEFAULT_DEPENDENCIES) + list(extra_dependencies) config = { "python_version": python_version, "graphs": {agent_name: "./graph.py:graph"}, "dependencies": deps, "env": ".env", } return json.dumps(config, indent=2) + "\n" def _render_graph_py(*, graph_factory_module: str, graph_factory_attr: str) -> str: return ( '"""LangGraph deployment entrypoint.\n' "\n" "Generated by ``langgoap deploy-init`` /\n" "``langgoap.integrations.langgraph_deploy.scaffold_deployment``.\n" "\n" "``langgraph dev`` and ``langgraph deploy`` look for the\n" "module-level ``graph`` symbol referenced from ``langgraph.json``.\n" "We import the user-supplied factory, call it to construct a\n" "configured :class:`~langgoap.graph.builder.GoapGraph`, then\n" "compile it — the resulting :class:`CompiledStateGraph` is what\n" "LangGraph serves over its routes (including ``/mcp``).\n" '"""\n' "\n" "from __future__ import annotations\n" "\n" f"from {graph_factory_module} import {graph_factory_attr}\n" "\n" "# Build + compile. LangGraph Platform and ``langgraph dev`` provide\n" "# persistence (checkpointer and store) themselves and ignore a\n" "# checkpointer passed to .compile(); configure it on the platform.\n" f"_goap_graph = {graph_factory_attr}()\n" "graph = _goap_graph.compile()\n" ) def _render_env_example() -> str: return ( "# Copy to .env and fill in. langgraph dev loads .env on startup.\n" "\n" "# LLM provider — adjust per the providers your actions call.\n" "OPENAI_API_KEY=\n" "\n" "# Optional: enable LangSmith tracing for the deployed agent.\n" "LANGSMITH_API_KEY=\n" "LANGSMITH_TRACING=true\n" "LANGSMITH_PROJECT=langgoap-deployment\n" ) def _render_readme(*, agent_name: str, agent_description: str) -> str: return ( f"# {agent_name}\n" "\n" f"{agent_description}\n" "\n" "Generated by `langgoap deploy-init`. The agent is a " "[LangGOAP](https://github.com/LangGOAP/LangGOAP) " "GoapGraph deployed via `langgraph dev`/`langgraph deploy`. " "Once deployed, **MCP routes are exposed by default** — any MCP " "client (Claude Desktop, IDE plugins, other agents) can discover " f"and call `{agent_name}` as a tool.\n" "\n" "## 1. Install\n" "\n" "```bash\n" 'uv pip install "langgraph-cli[inmem]" langgoap\n' "cp .env.example .env # fill in OPENAI_API_KEY, etc.\n" "```\n" "\n" "## 2. Run locally\n" "\n" "```bash\n" "langgraph dev\n" "```\n" "\n" f"This brings up the LangGraph API server with the `{agent_name}` " "graph deployed. The MCP endpoint is at `/mcp` (enabled by default).\n" "\n" "## 3. Connect an MCP client\n" "\n" "### Claude Desktop\n" "\n" "Add to `~/.config/Claude/claude_desktop_config.json`:\n" "\n" "```json\n" "{\n" ' "mcpServers": {\n' f' "{agent_name}": {{\n' ' "transport": {\n' ' "type": "http",\n' ' "url": "http://127.0.0.1:2024/mcp"\n' " }\n" " }\n" " }\n" "}\n" "```\n" "\n" f"Restart Claude Desktop. The `{agent_name}` agent appears as " "a tool. Ask Claude to use it.\n" "\n" "### Other MCP clients\n" "\n" "Any MCP-compliant client (IDE plugins, programmatic SDKs, " "competing agents) can call the `/mcp` endpoint identically — " "the protocol is transport-agnostic.\n" ) __all__ = [ "DEFAULT_DEPENDENCIES", "scaffold_deployment", ]