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

# 如何添加自定义路由

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

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

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

以下是 Python 和 Javascript 的示例。

## 创建应用

从**现有**的 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="Javascript">
    ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    yarn create langgraph
    yarn add hono
    ```

    拥有 LangGraph 项目后，添加以下应用代码：

    ```js theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    // ./src/agent/app.ts
    import { Hono } from "hono";

    export const app = new Hono();

    app.get("/hello", (c) => c.json({ hello: "world" }));
    ```
  </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"
      }
      // 其他配置选项，如认证、存储等。
    }
    ```
  </Tab>

  <Tab title="Javascript">
    ```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    {
      "graphs": {
        "agent": "./src/agent/graph.ts:graph"
      },
      "env": ".env",
      "http": {
        "app": "./src/agent/app.ts:app"
      }
      // 其他配置选项，如认证、存储等。
    }
    ```
  </Tab>
</Tabs>

## 启动服务器

在本地测试服务器：

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

  <Tab title="Javascript">
    ```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 响应。

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

## 部署

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

## 后续步骤

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

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\langsmith\custom-routes.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
