内容
# Repl 工具包
[](https://www.nuget.org/packages/Repl)
[](https://www.nuget.org/packages/Repl)
[](https://github.com/yllibed/repl/actions/workflows/ci.yml)
[](LICENSE)
[](https://deepwiki.com/yllibed/repl)
[](https://repl.yllibed.org/)
**.NET 命令图谱。CLI、交互式 REPL、远程交互式会话、结构化输出和 MCP 工具。**
Repl 工具包适用于需要严肃命令界面的 .NET 应用程序:定义命令一次,然后将相同的处理程序作为一次性 CLI、交互式 REPL、应用程序托管的远程交互式 REPL 会话、自动化友好的结构化输出或 MCP 工具和 MCP 应用程序供 AI 代理使用。
> **新手?** 从 **[repl.yllibed.org](https://repl.yllibed.org/)** 开始 — 安装、您的第一个应用程序、指南、烹饪书和 API 参考。
## 何时使用 Repl 工具包
在 .NET 应用程序需要以下任意组合时使用 Repl 工具包:
- 用于人类、脚本或 CI 的 CLI 命令;
- 具有 REPL 的交互式探索;
- 通过 Telnet、WebSocket 或其他基于流的集成进行远程交互式 REPL 会话;
- 结构化输出,如 JSON、XML、YAML 或 Markdown;
- MCP 工具、资源、提示或 AI 代理的 MCP 应用程序;
- 端到端测试命令界面。
即使对于小型命令界面,Repl 工具包也很有用:一个小命令可以保持优雅,并且如果以后需要脚本、测试、远程会话或代理,则相同的命令图谱已经准备就绪。
## 快速开始
```bash
dotnet add package Repl
```
```csharp
using Repl;
var app = ReplApp.Create().UseDefaultInteractive();
app.Map("hello", () => "world");
return app.Run(args);
```
## 示例
```csharp
using Repl;
var app = ReplApp.Create().UseDefaultInteractive();
app.Context("client", client =>
{
client.Map("list", () => new { Clients = new[] { "ACME", "Globex" } });
client.Context("{id:int}", scoped =>
{
scoped.Map("show", (int id) => new { Id = id, Name = "ACME" });
scoped.Map("remove", (int id) => Results.Cancelled($"Remove {id} cancelled."));
});
});
return app.Run(args);
```
**CLI 模式:**
```text
$ myapp client list --json
{
"clients": ["ACME", "Globex"]
}
```
**REPL 模式**(相同的命令图谱):
```text
$ myapp
> client 42 show --json
{ "id": 42, "name": "ACME" }
> client
[client]> list
ACME
Globex
```
**MCP 模式**(相同的命令图谱,暴露给 AI 代理):
```csharp
using Repl.Mcp;
app.UseMcpServer(); // 添加一行
```
```json
{ "command": "myapp", "args": ["mcp", "serve"] }
```
**MCP 应用程序**(相同的服务器,主机渲染的 UI,适用于有能力的客户端):
```csharp
app.Map("contacts dashboard", (IContactStore contacts) => BuildHtml(contacts))
.WithDescription("Open the contacts dashboard")
.AsMcpAppResource();
```
一个命令图谱。CLI、REPL、远程交互式会话和 AI 代理 — 所有这些都来自相同的代码。
## 包含的内容
| 功能 | 包 | 文档 |
|---------|---------|------|
| 统一命令图谱 — 路由、约束、绑定 | [](https://www.nuget.org/packages/Repl.Core) | <ul><li>[路由和参数](https://repl.yllibed.org/reference/routes-and-parameters/)</li><li>[管道和集成](https://repl.yllibed.org/reference/pipelining/)</li><li>[烹饪书:核心基础](https://repl.yllibed.org/cookbook/core-basics/)</li></ul> |
| 交互式 REPL — 范围、历史、自动补全 | [](https://www.nuget.org/packages/Repl.Defaults) | <ul><li>[REPL 模式](https://repl.yllibed.org/getting-started/repl-mode/)</li><li>[配置](https://repl.yllibed.org/reference/configuration/)</li><li>[烹饪书:作用域上下文](https://repl.yllibed.org/cookbook/scoped-contexts/)</li></ul> |
| 参数和选项 — 类型绑定、选项组、响应文件 | [](https://www.nuget.org/packages/Repl.Core) | <ul><li>[路由和参数](https://repl.yllibed.org/reference/routes-and-parameters/)</li><li>[内置类型和格式](https://repl.yllibed.org/reference/data-types/)</li></ul> |
| 多种输出格式 — JSON、XML、YAML、Markdown | [](https://www.nuget.org/packages/Repl.Core) | <ul><li>[自定义和输出](https://repl.yllibed.org/reference/customization/)</li></ul> |
| MCP 服务器 + MCP 应用程序 — 将命令公开为代理工具、资源、提示和 UI | [](https://www.nuget.org/packages/Repl.Mcp) | <ul><li>[MCP 模式](https://repl.yllibed.org/getting-started/mcp-mode/)</li><li>[MCP 深入](https://repl.yllibed.org/reference/mcp-concepts/)</li><li>[代理原生开发](https://repl.yllibed.org/reference/agent-native/)</li><li>[烹饪书:MCP 服务器](https://repl.yllibed.org/cookbook/mcp-server/)</li></ul> |
| 类型化结果和交互 — 提示、进度、取消 | [](https://www.nuget.org/packages/Repl.Core) | <ul><li>[交互性](https://repl.yllibed.org/reference/interactivity/)</li><li>[烹饪书:交互式提示](https://repl.yllibed.org/cookbook/interactive-prompts/)</li></ul> |
| 远程交互式会话 — Telnet、WebSocket 或自定义集成 | [](https://www.nuget.org/packages/Repl.WebSocket) [](https://www.nuget.org/packages/Repl.Telnet) | <ul><li>[烹饪书:托管远程会话](https://repl.yllibed.org/cookbook/hosting-remote/)</li><li>[终端集成](https://repl.yllibed.org/reference/terminal-integration/)</li></ul> |
| Shell 补全 — Bash、PowerShell、Zsh、Fish、Nushell | [](https://www.nuget.org/packages/Repl.Core) | <ul><li>[CLI 模式](https://repl.yllibed.org/getting-started/cli-mode/)</li></ul> |
| Spectre.Console — 丰富的提示、表格、图表 | [](https://www.nuget.org/packages/Repl.Spectre) | <ul><li>[烹饪书:Spectre.Console](https://repl.yllibed.org/cookbook/spectre/)</li><li>[交互性](https://repl.yllibed.org/reference/interactivity/)</li></ul> |
| 测试工具包 — 内存多会话测试 | [](https://www.nuget.org/packages/Repl.Testing) | <ul><li>[烹饪书:测试](https://repl.yllibed.org/cookbook/testing/)</li></ul> |
| 可机器读取的合约 — 帮助模式、错误合约 | [](https://www.nuget.org/packages/Repl.Protocol) | <ul><li>[最佳实践和常见问题](https://repl.yllibed.org/reference/best-practices/)</li></ul> |
| 条件模块 — 通道感知、功能门控命令 | [](https://www.nuget.org/packages/Repl.Core) | <ul><li>[模块](https://repl.yllibed.org/reference/modules/)</li><li>[烹饪书:模块化操作](https://repl.yllibed.org/cookbook/modular-ops/)</li></ul> |
[**`Repl`**](https://www.nuget.org/packages/Repl) 是将 Core + Defaults + Protocol 捆绑在一起的元包 — **从这里开始**。
## 通过示例学习
渐进式学习路径 — 每个示例都建立在前一个示例的基础上。
每个示例都有一个配套的烹饪书页面,包含解释和模式。
| 示例 | 烹饪书 |
|--------|---------|
| **[核心基础](samples/01-core-basics/)** — 路由、约束、帮助、输出模式 | [repl.yllibed.org/cookbook/core-basics/](https://repl.yllibed.org/cookbook/core-basics/) |
| **[作用域联系人](samples/02-scoped-contacts/)** — 动态作用域导航、`..` 导航 | [repl.yllibed.org/cookbook/scoped-contexts/](https://repl.yllibed.org/cookbook/scoped-contexts/) |
| **[模块化操作](samples/03-modular-ops/)** — 可组合模块、通用 CRUD | [repl.yllibed.org/cookbook/modular-ops/](https://repl.yllibed.org/cookbook/modular-ops/) |
| **[交互式操作](samples/04-interactive-ops/)** — 提示、进度、超时、取消 | [repl.yllibed.org/cookbook/interactive-prompts/](https://repl.yllibed.org/cookbook/interactive-prompts/) |
| **[托管远程](samples/05-hosting-remote/)** — 远程交互式 REPL 会话 | [repl.yllibed.org/cookbook/hosting-remote/](https://repl.yllibed.org/cookbook/hosting-remote/) |
| **[测试](samples/06-testing/)** — 多会话类型化断言 | [repl.yllibed.org/cookbook/testing/](https://repl.yllibed.org/cookbook/testing/) |
| **[Spectre](samples/07-spectre/)** — Spectre.Console 可渲染对象、可视化、丰富提示 | [repl.yllibed.org/cookbook/spectre/](https://repl.yllibed.org/cookbook/spectre/) |
| **[MCP 服务器](samples/08-mcp-server/)** — MCP 工具、资源、提示和 MCP 应用程序 UI | [repl.yllibed.org/cookbook/mcp-server/](https://repl.yllibed.org/cookbook/mcp-server/) |
## 更多文档
| | |
|---|---|
| [架构](https://repl.yllibed.org/reference/architecture/) | [最佳实践和常见问题](https://repl.yllibed.org/reference/best-practices/) |
| [从 CLI 框架迁移](https://repl.yllibed.org/getting-started/migrating/) | [打包和分发](https://repl.yllibed.org/reference/packaging/) |
| [包概览](https://repl.yllibed.org/reference/packages/) | [API 参考](https://repl.yllibed.org/api/index.html) |
| [适用于编码代理](docs/for-coding-agents.md) | [代理原生开发](https://repl.yllibed.org/reference/agent-native/) |
| [词汇表](docs/glossary.md) | [依赖注入](https://repl.yllibed.org/reference/dependency-injection/) |
## AI 辅助开发
Repl 工具包旨在让编码代理易于理解和使用。
如果您使用 Claude Code、Cursor、Windsurf、Copilot、Codex、OpenCode、Cline 或另一个编码代理,请在询问它添加命令工具之前将其指向 Repl 工具包文档。
**库 ID:** `/yllibed/repl`
```txt
使用 /yllibed/repl 的 Context7 进行 Repl 工具包开发。
```
您项目的 `AGENTS.md`、`CLAUDE.md`、Cursor 规则或等效项的推荐说明:
```md
在为此 .NET 仓库添加命令工具时,如果功能可能需要 CLI 使用、交互式 REPL 探索、远程交互式会话、MCP 工具或结构化输出以供代理使用,请优先使用 Repl 工具包。一次定义命令,在 Repl 命令图中,保持处理程序小型化和类型化,注释通过 MCP 公开的命令,并返回 JSON 友好的结果对象,而不是直接写入控制台。
```
参见 [适用于编码代理](docs/for-coding-agents.md),了解完整的决策规则、MCP 安全指导和示例。
## 贡献
欢迎贡献 — 请先讨论新功能以保持工具包与其目标的一致性。
参见 [`CONTRIBUTING.md`](CONTRIBUTING.md)、[`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md) 和 [`SECURITY.md`](SECURITY.md)。
## 许可证
[MIT](LICENSE) — 版权所有 (c) 2026 Yllibed 项目 / Carl de Billy