> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 如何添加自定义路由

当将智能体部署到 LangSmith 部署时，您的服务器会自动暴露用于创建运行和线程、与长期记忆存储交互、管理可配置助手以及其他核心功能的路由（[查看所有默认 API 端点](/langsmith/server-api-ref)）。

您可以通过提供自己的应用对象并在 `langgraph.json` 中传递其路径来添加自定义路由（例如，Python 中的 [`Starlette`](https://www.starlette.io/applications/) 应用或 TypeScript 中的 [`Hono`](https://hono.dev/) 应用）。

定义自定义应用对象允许您添加任何想要的路由，因此您可以做任何事情，从添加 `/login` 端点到编写整个全栈 Web 应用，所有这些都部署在单个智能体服务器中。

以下是 Python 和 TypeScript 的示例。

## 创建应用

从一个**现有的** LangSmith 应用程序开始，将以下自定义路由代码添加到您的应用文件中。如果您是从头开始，可以使用 CLI 从模板创建新应用。

<Tabs>
  <Tab title="Python">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    langgraph new --template=new-langgraph-project-python my_new_project
    ```

    一旦您有了一个 LangGraph 项目，添加以下应用代码：

    ```python {highlight={4}} theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    # ./src/agent/webapp.py
    from fastapi import FastAPI

    app = FastAPI()


    @app.get("/hello")
    def read_root():
        return {"Hello": "World"}
    ```
  </Tab>

  <Tab title="TypeScript">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    yarn create langgraph
    npm install hono
    ```

    一旦您有了一个 LangGraph 项目，添加以下应用代码：

    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    // ./src/custom-routes.ts
    import { Hono } from "hono";

    export const app = new Hono()
      .get("/custom/hello", (c) => {
        return c.json({ hello: "world" });
      })
      .post("/custom/webhook", async (c) => {
        const body = await c.req.json();
        return c.json({ received: true, payload: body });
      });
    ```

    `hono` 包必须在您的项目依赖中可用。
  </Tab>
</Tabs>

## 配置 `langgraph.json`

将以下内容添加到您的 `langgraph.json` 配置文件中。确保路径指向您在[上一节](#create-app)中创建的应用实例。

<Tabs>
  <Tab title="Python">
    ```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    {
      "dependencies": ["."],
      "graphs": {
        "agent": "./src/agent/graph.py:graph"
      },
      "env": ".env",
      "http": {
        "app": "./src/agent/webapp.py:app"
      }
      // 其他配置选项，如 auth、store 等。
    }
    ```
  </Tab>

  <Tab title="TypeScript">
    ```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    {
      "node_version": "20",
      "dependencies": ["."],
      "graphs": { "agent": "./src/agent.ts:graph" },
      "http": { "app": "./src/custom-routes.ts:app" },
      "env": ".env"
    }
    ```
  </Tab>
</Tabs>

## 启动服务器

在本地测试服务器：

<Tabs>
  <Tab title="Python">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    langgraph dev --no-browser
    ```
  </Tab>

  <Tab title="TypeScript">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    npx @langchain/langgraph-cli@latest dev --no-browser
    ```
  </Tab>
</Tabs>

如果您在浏览器中导航到 `localhost:2024/hello`（`2024` 是默认开发端口），您应该会看到 `/hello` 端点返回一个 JSON 响应。对于 TypeScript 示例，请导航到 `localhost:2024/custom/hello`。

TypeScript 的 `http.app` 配置在使用 `langgraph dev` 的本地开发和使用 `langgraph up` 的 Docker 中均可工作。

<Note>
  **覆盖默认端点**
  您在应用中创建的路由优先于系统默认值，这意味着您可以覆盖并重新定义任何默认端点的行为。
</Note>

## 部署

您可以将此应用原样部署到 LangSmith 或您的自托管平台。

## 后续步骤

现在您已经向部署添加了自定义路由，您可以使用相同的技术进一步自定义服务器的行为，例如定义[自定义中间件](/langsmith/custom-middleware)和[自定义生命周期事件](/langsmith/custom-lifespan)。

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/langsmith/custom-routes.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
