跳转到主要内容

Documentation Index

Fetch the complete documentation index at: https://ahvn.top/llms.txt

Use this file to discover all available pages before exploring further.

最好的画布,是空白的画布。
在本教程中,我们创建一个空的 HeavenBase 工作区,并将其暴露为 MCP 服务器。这样,任何使用 HeavenBase MCP 的 agent 都能把它当作数据平面与结构化记忆服务。由 agent 决定如何存储观察结果、存储什么、以及如何查询;MCP 服务器负责其余部分。 教程结束时,我们将运行一个玩具电商场景:agent 即时定义 Product、Customer、Order 实体,并通过日常操作维护数据库,例如添加商品、注册客户、记录订单。最后,agent 回答业务分析问题,例如「售价低于 $100 的商品占比多少?」以及「根据 Alice 的订单历史,我该向她推荐什么?」

1. 创建空工作区

创建一个 Python 文件:
# serve_space.py
import heavenbase as hb

# If you've already run this tutorial once, this drops the existing workspace so you can start fresh.
# Remove this line if you want the workspace to persist across server restarts.
hb.HeavenBase("my-space", preset="debug").drop()

ws = hb.HeavenBase("my-space", preset="debug")

print(
    ws.to_mcp_json(
        name="my-space-mcp",
        profile="agent",
        transport="http",
        host="127.0.0.1",
        port=7001
    )
)

ws.serve(
    name="my-space-mcp",
    profile="agent",
    transport="http",
    host="127.0.0.1",
    port=7001
)
无需实体、字段或 schema。HeavenBase 的通用 MCP 工具会动态处理一切。
hb.HeavenBase("my-space", preset="debug").drop() 会在服务器启动前清空演示工作区。教程需要可重复运行时请保留此行;若希望工作区数据在服务器重启后仍保留,请删除该行。
preset 参数控制后端配置。debug 预设使用 SQLite 与内存存储,便于轻量、免 Docker 搭建。local-lts 预设使用本机 Postgres、LanceDB 与 Elasticsearch,更稳健且仍在本机运行。两种预设均支持完整 MCP 工具集:debug 侧重易用,local-lts 在更大数据量下性能更好。 profile 参数选择服务器暴露的工作区 MCP 工具集。HeavenBase 内置目前提供 full(可信管理流程)、agent(首日 schema 与数据工作)、memory(笔记式记忆)与 memstate(项目级记忆状态)。 本页使用 profile="agent",因为 agent 可用其定义实体、检查 schema、upsert 行、patch 行、计数、查询并解释路由,同时省略批量操作、存在性检查与删除。

2. 运行

python serve_space.py
服务器会打印客户端配置并开始监听:
{
  "mcpServers": {
    "my-space-mcp": {
      "transport": "http",
      "url": "http://127.0.0.1:7001/mcp"
    }
  }
}
示例使用 /mcp 上的 Streamable HTTP。HeavenBase 支持常见 MCP 传输方式。每个服务器进程只使用一种传输,客户端连接对应端点:
TransportPython serve callClient endpoint
Streamable HTTPtransport="http"http://127.0.0.1:7001/mcp
SSEtransport="sse"http://127.0.0.1:7001/sse
stdiotransport="stdio"command transport, no URL
在 Python 中,对 ws.to_mcp_json(...)ws.serve(...) 传入相同 transport。例如,仅支持 SSE 的客户端需要 /sse 时,两处均使用 transport="sse"
工作区 MCP 服务器绑定实时工作区对象。外部 agent 使用时请保持 serve_space.py 运行。与持久化的 Toolkit 注册表引用不同,空工作区 MCP 服务器是暴露该脚本所创建工作区的常驻进程。

3. 连接你的 Agent

将服务器添加到你常用的编程 agent:
使用 Claude Code CLI 添加 HTTP 服务器:
claude mcp add --transport http my-space-mcp http://127.0.0.1:7001/mcp
claude mcp list
Claude Code 将本地作用域 MCP 服务器保存在 ~/.claude.json。若需在仓库内共享,在项目根目录用相同命令加 --scope project,或创建 .mcp.json
{
  "mcpServers": {
    "my-space-mcp": {
      "type": "http",
      "url": "http://127.0.0.1:7001/mcp"
    }
  }
}
开启新的 Claude Code 会话,运行 /mcp,在让 agent 使用算术工具前确认 my-space-mcp 已连接。

4. 动手体验:玩具电商场景

你的 agent 会发现 HeavenBase 的 agent MCP profile。它暴露一小套 schema 与数据能力:
ToolWhat it offers
define_entity根据 JSON 兼容 schema 创建实体定义。
list_entities列出 agent 可检查的工作区实体。
describe_entity返回某一实体的字段、逻辑类型与路由计划。
upsert为某一实体插入或替换一行。
get按对象 ID 获取一行。
set修补一行并返回更新后的行。
count统计某一实体的行数。
query运行带过滤、投影、排序与限制的 JSON 查询。
explain展示查询的路由与处理器计划。
agent profile 有意省略批量变更、批量读取、存在性检查与删除。仅在可信管理流程中使用 profile="full" 按顺序将下方每条提示复制到 agent 对话中。每个代码块是一条可粘贴发送的消息。
1

设置工作区上下文

告诉 agent 使用哪个 MCP 服务器:
Use the `my-space-mcp` MCP for maintaining an e-commerce knowledgebase.
开启新会话时,建议先粘贴此提示,以设定最小工作区上下文。
2

定义业务模型

请 agent 逐个创建核心实体:
Create a Product entity schema with name, price, category (textual), and availability (default to true).
Create a Customer entity schema with name, email, address (textual), profile (list of texts about the customer).
Create an Order entity schema with order_id, customer_id (reference), product_id (reference), quantity. One order row holds one product; use multiple rows when an order has multiple products.
3

日常运营

向工作区写入目录商品、客户与订单:
Add a product: "Ergonomic Keyboard" priced at $89.99 in category "electronics".
Add a product: "27-inch 4K Monitor" priced at $349.00 in category "electronics".
Add a product: "Bamboo Desk Organizer" priced at $24.50 in category "office".
Bob (bob@gmail.com, 456 Oak St) registered but hasn't ordered anything yet.
Bob searched for "chair" and didn't find anything.
Alice (alice@gmail.com, 123 Maple St) ordered 2 keyboards and 1 monitor.
Add a product: "Mesh Office Chair" priced at $199.99 in category "office".
Add a product: "Wooden Chair" priced at $39.99 in category "office".
Add a product: "Wireless Mouse" priced at $49.99 in category "electronics".
Update Bob's address to 789 Pine St.
Wood chair is out of stock.
4

分析问题

提出分析问题。每条提示下方为预期答案。
What percentage of products in my store cost less than $100?
66.67% (4 out of 6 products)
List all products in the electronics category sorted by price ascending.
Wireless Mouse ($49.99), Ergonomic Keyboard ($89.99), 27-inch 4K Monitor ($349.00)
What's the best-selling product based on total quantity ordered?
Ergonomic Keyboard (2)
How many active customers (at least one order) do I have?
1 (Alice)
What products should I recommend to Alice and Bob based on their profiles and order history? Write a short recommendation pop-up for each of them.
  • Hi Alice — complete your desk setup
  • You already run a keyboard-and-monitor setup. Add a Wireless Mouse ($49.99) for a matched input stack, and a Bamboo Desk Organizer ($24.50) to tidy cables and accessories.
  • Suggested add-ons: Wireless Mouse · Bamboo Desk Organizer

  • Hi Bob — we found chairs for you
  • You searched for a chair earlier. The Wooden Chair isn’t available right now, but the Mesh Office Chair ($199.99) is — ergonomic mesh for long sessions.
  • Suggested for you: Mesh Office Chair
现在你拥有供 agent 使用的持久、可查询、结构化记忆平面。这个电商玩具示例全程无需你编写任何 CRUD 函数或业务逻辑:agent 检查可用工具、即时定义实体、写入数据,并通过同一 MCP 接口运行查询。

进一步探索

相关资源: