tiny-engine icon indicating copy to clipboard operation
tiny-engine copied to clipboard

feat: add base resource tools

Open chilingling opened this issue 3 months ago • 1 comments

English | 简体中文

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our Commit Message Guidelines
  • [ ] Tests for the changes have been added (for bug fixes / features)
  • [ ] Docs have been added / updated (for bug fixes / features)
  • [ ] Built its own designer, fully self-validated

PR Type

What kind of change does this PR introduce?

  • [ ] Bugfix
  • [x] Feature
  • [ ] Code style update (formatting, local variables)
  • [ ] Refactoring (no functional changes, no api changes)
  • [ ] Build related changes
  • [ ] CI related changes
  • [ ] Documentation content changes
  • [ ] Other... Please describe:

Background and solution

What is the current behavior?

新增 MCP 资源支持

  • 支持 MCP 资源自动收集&注册 (注册表声明然后自动收集注册)
  • 支持 MCP 管理 API:enable(启用)、disable(禁用)、register(手动注册)
  • 支持资源读取基础工具类
    • discover_resources:资源发现,查看 TinyEngine 资源列表
    • read_resources:读取资源,读取具体某一个资源的详情
    • search_resources:搜索资源,根据筛选条件判断进行资源搜索

新增 MCP 资源

  • 页面 Schema 协议文档
  • 编辑页面 schema 示例文档

新增编辑页面 schema 工具类

  • 支持编辑完整的页面 schema
  • 支持部分编辑,比如仅编辑 CSS、lifecycle、method、state

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • [ ] Yes
  • [x] No

Other information

Summary by CodeRabbit

  • New Features

    • Added “Edit Page Schema” tool (merge/replace for state, CSS, methods, lifecycles) with validations and structured feedback.
    • Introduced resource system: discover, search, and read tools (filters, pagination, snippets) plus per-section resource templates and a sequential-thinking helper.
  • Documentation

    • Added guides: Page Schema Protocol, edit-page schema examples, and TinyEngine 操作指南 — all accessible as full documents or per-section resources.
  • Chores

    • Updated assistant system prompt to a concise, Chinese, tool-first workflow.

chilingling avatar Sep 22 '25 13:09 chilingling

Walkthrough

Adds MCP resource management and discovery infrastructure, new base tools (discover/read/search/sequentialThinking), extensive design-doc resources with per-section templates, an EditPageSchema tool suite for editing page schema sections, and integrates resources/tools into MCP state and DesignCanvas exports.

Changes

Cohort / File(s) Change Summary
DesignCanvas MCP entry
packages/canvas/DesignCanvas/src/mcp/index.ts
Imports EditPageSchema and resourcesExport; extends default export to include EditPageSchema in tools and adds top-level resources and resourceTemplates.
EditPageSchema tool suite
packages/canvas/DesignCanvas/src/mcp/tools/index.ts, .../editPageSchema/index.ts, .../editPageSchema/editState.ts, .../editPageSchema/editLifeCycleOrMethod.ts, .../editPageSchema/editCSS.ts, .../editPageSchema/editSchema.ts, .../editPageSchema/utils.ts
Adds edit_page_schema tool with zod-validated input, per-section handlers (state, lifeCycles, methods, css, schema), validation/util helpers, and re-exports EditPageSchema.
DesignCanvas resources (Markdown + templates)
packages/canvas/DesignCanvas/src/mcp/resources/pageSchema.md, .../pageSchemaProtocol.ts, .../editPageSchemaExample.md, .../editPageSchemaExample.ts, .../tinyEngineAIInstruct.md, .../tinyEngineAIInstruct.ts, .../utils.ts, .../index.ts
Adds markdown docs (page schema, examples, AI instruct), utilities to extract sections (pickSectionByHeading), and resource/resourceTemplate descriptors with per-section template callbacks; aggregates resources into resources and resourceTemplates.
MCP base tools (resources & utilities)
packages/common/composable/mcp/baseTools/discoverResources.ts, .../readResources.ts, .../searchResources.ts, .../utils.ts, .../sequentialThinking.ts, .../index.ts
Introduces base tools for discover/read/search resources, shared utilities for remote list fetching, content reading/truncation/validation, a sequentialThinking tool, and an aggregator getBaseTools.
MCP core wiring (state/APIs/init)
packages/common/composable/mcp/index.ts, packages/common/composable/mcp/resources.ts, packages/common/composable/mcp/toolUtils.ts, packages/common/composable/mcp/type.ts
Expands IState with resources, resourceTemplates, resourceInstanceMap; adds resource management APIs (register/get/list/remove/update), init helpers (initRegisterResources, initRegisterTools), and wiring to register resources and tools during server init.
MCP types
packages/common/composable/mcp/type.ts
Adds public types: ResourceContent, VariableSpec, ResourceItem, ResourceTemplateItem; updates imports to include MCP server resource typings.
Plugins prompt
packages/plugins/robot/src/system-prompt.md
Replaces system prompt with a Chinese, tool-first TinyEngine Assistant system prompt.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant App as App
  participant MCP as MCP Core
  participant Tools as initRegisterTools
  participant Res as initRegisterResources
  participant Server as McpServer

  App->>MCP: createServer(state)
  MCP->>MCP: collectTools() + collectResources()
  MCP->>Server: instantiate server
  MCP->>Tools: initRegisterTools(state, server)
  Tools->>Server: registerTool(name, config, cb)
  MCP->>Res: initRegisterResources(state, server)
  Res->>Server: registerResource / registerResourceTemplate
  Res->>Server: sendResourceListChanged()
  MCP-->>App: server + APIs ready (tools + resource APIs)
sequenceDiagram
  autonumber
  participant Client as Client Tool Call
  participant ReadTool as read_resources
  participant Utils as readResourceContent
  participant Remote as mcpClient

  Client->>ReadTool: { uri | uriTemplate+variables }
  alt uriTemplate mode
    ReadTool->>ReadTool: resolve final URI
  end
  ReadTool->>Utils: readResourceContent(uri, limits)
  Utils->>Remote: readResource(uri)
  alt success (textual)
    Utils->>Utils: validate + truncate (if needed)
    Utils-->>ReadTool: { ok, contents, truncated? }
    ReadTool-->>Client: JSON payload (status, data)
  else failure
    Utils-->>ReadTool: { ok: false, error }
    ReadTool-->>Client: JSON error payload
  end
sequenceDiagram
  autonumber
  participant Client as Client Tool Call
  participant Search as search_resources
  participant Lists as tryFetchRemoteLists
  participant Read as readResourceWithFallback
  participant Remote as mcpClient

  Client->>Search: { query, scope, filters }
  Search->>Lists: fetch resources/templates
  alt scope includes content
    loop for candidate resources
      Search->>Read: readResourceWithFallback(uri)
      Read->>Remote: readResource(uri)
      Read-->>Search: contents or error
      Search->>Search: score by content/snippet
    end
  else metadata-only
    Search->>Search: score by metadata only
  end
  Search-->>Client: topK results (score, snippet?, metadata)

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~75 minutes

Poem

I nibble notes and stitch the rails,
New docs and tools in tidy trails.
Sections found and schemas tuned,
Searches hum and resources pruned.
Hop, I share this patch with cheer—canvas bright, the path is clear. 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title “feat: add base resource tools” concisely captures a significant aspect of the changeset by highlighting the addition of core resource discovery, reading, and search tools in the baseTools modules, and it accurately reflects substantive new functionality without including irrelevant details.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • [ ] 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bf0a0c3c17507b1ce7a707880aca9065ad35a6fd and c190d51745da099a72452d7ff55261be2af062d6.

📒 Files selected for processing (2)
  • packages/canvas/DesignCanvas/src/mcp/resources/pageSchema.md (1 hunks)
  • packages/canvas/DesignCanvas/src/mcp/resources/pageSchemaProtocol.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/canvas/DesignCanvas/src/mcp/resources/pageSchemaProtocol.ts
  • packages/canvas/DesignCanvas/src/mcp/resources/pageSchema.md
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: push-check

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Sep 22 '25 13:09 coderabbitai[bot]