Files
LangBot/skills/src/commands/trouble.ts
T
Junyan Chin e9dd584792 feat: MCP server + in-repo skills (agent-friendly platform) (#2269)
* feat(api): support global API key from config.yaml (api.global_api_key)

Accept a config-defined global API key anywhere a web-UI key is accepted
(X-API-Key / Bearer), with no login session and no DB record. Useful for
automated deployments and AI agents (HTTP API + MCP). Defaults to empty
(disabled); does not require the lbk_ prefix.

- templates/config.yaml: add api.global_api_key with security notes
- service/apikey.py: verify_api_key checks global key first (constant-time)
- docs/API_KEY_AUTH.md: document the global key + security guidance
- tests: cover global-key match, prefix-free, fallback-to-db, disabled

* feat(mcp): expose LangBot management as an MCP server at /mcp

Add an MCP (Model Context Protocol) server so external AI agents can manage a
LangBot instance. Reuses the same API-key auth as the HTTP API (including the
config.yaml global API key).

- pkg/api/mcp/server.py: FastMCP server wrapping the service layer; 21 curated
  tools across system/bots/pipelines/models/knowledge/mcp-servers/skills
- pkg/api/mcp/mount.py: ASGI dispatcher fronting Quart; authenticates /mcp
  requests with an API key, runs the streamable-HTTP session manager lifespan
- controller/main.py: serve the wrapped ASGI app via hypercorn (was run_task)
- web: new 'MCP' tab in the API integration dialog showing endpoint, auth, and
  client config; i18n for 8 locales
- tests/manual/mcp_smoke.py: e2e check (401 unauth, list tools, call tools)

Tool surface is intentionally curated (not all ~25 route groups) to keep the
agent surface small, safe, and maintainable. Extend deliberately.

* feat(skills): add in-repo skills/ as the single source of truth

Migrate the agent skills + QA/e2e test harness from the (now archived)
langbot-app/langbot-skills repo into LangBot/skills/, and add four new skills.

Migrated:
- langbot-plugin-dev, langbot-testing (e2e), langbot-env-setup,
  langbot-skills-maintenance, langbot-eba-adapter-dev
- the bin/lbs CLI (src/, test/, scripts/, schemas/, qa-agent-docs/)

New:
- langbot-dev      core backend + web development
- langbot-deploy   Docker/K8s deployment + config.yaml + global API key
- langbot-mcp-ops  operating the LangBot MCP server (/mcp)
- langbot-space-ops operating the Space marketplace MCP server

- src/cli.ts repoRoot(): recognize the skills assets root (skills.index.json +
  bin/lbs) so the CLI works when nested inside the LangBot repo
- README.md: unified skill catalog; skills.index.json regenerated

Parity with source verified: bin/lbs validate + node test suite match the
source repo (only the uncommitted .lbpkg build-artifact fixture differs).

* docs(agents): document agent-facing surfaces + API/MCP/skills sync rule

* docs(readme): add 'Built for AI Agents' section across all locales

Highlight MCP server, in-repo skills (single source of truth), AGENTS.md
sync rule, and llms.txt. Cross-link LangBot Space MCP marketplace.

* style(mcp): fix ruff format + prettier lint in MCP server and API panel

* style(web): prettier format MCP i18n locale entries

* docs(skills): note MCP instance control in dev/testing skills

All development-guidance skills now point to the LangBot instance MCP
server (/mcp) and the Space marketplace MCP server, reusing API keys.
2026-06-20 15:14:47 +08:00

96 lines
3.8 KiB
TypeScript

import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import type { CommandContext } from "../types.ts";
import { fail, optionString, parseOptions, usage } from "../cli.ts";
import { findStructuredItem, getSkill, loadStructuredItems, scalar, slugify, todayIso, yamlList, yamlQuote } from "../fs.ts";
function troubleshootingYamlPath(root: string, skillName: string, id: string): string {
const skill = getSkill(root, skillName);
const dir = join(skill.path, "troubleshooting");
mkdirSync(dir, { recursive: true });
return join(dir, `${id}.yaml`);
}
function legacyTroubleshootingPath(root: string, skillName: string): string {
const skill = getSkill(root, skillName);
const refsDir = join(skill.path, "references");
mkdirSync(refsDir, { recursive: true });
const path = join(refsDir, "troubleshooting.md");
if (!existsSync(path)) writeFileSync(path, "# Troubleshooting\n\n", "utf8");
return path;
}
export function commandTroubleList(ctx: CommandContext): number {
const skill = ctx.args[2];
const yamlItems = loadStructuredItems(ctx.root, "troubleshooting", skill);
for (const item of yamlItems) {
console.log(`${item.skill}\t${scalar(item.fields, "id")}\t${scalar(item.fields, "title")}`);
}
if (skill && yamlItems.length === 0) {
const legacyPath = legacyTroubleshootingPath(ctx.root, skill);
const text = readFileSync(legacyPath, "utf8");
const headings = Array.from(text.matchAll(/^##\s+(.+)$/gm)).map((match) => match[1]);
for (const heading of headings) console.log(`${skill}\tlegacy\t${heading}`);
}
return 0;
}
export function commandTroubleShow(ctx: CommandContext): number {
const positional = ctx.args.slice(2);
if (positional.length < 1 || positional.length > 2) usage();
const item = positional.length === 1
? findStructuredItem(ctx.root, "troubleshooting", positional[0])
: findStructuredItem(ctx.root, "troubleshooting", positional[0], positional[1]);
console.log(item.raw.trimEnd());
return 0;
}
export function commandTroubleSearch(ctx: CommandContext): number {
const query = ctx.args[2]?.toLowerCase();
if (!query) usage();
const items = loadStructuredItems(ctx.root, "troubleshooting").filter((item) => item.raw.toLowerCase().includes(query));
for (const item of items) {
console.log(`${item.skill}\t${scalar(item.fields, "id")}\t${scalar(item.fields, "title")}`);
}
return 0;
}
export function commandTroubleAdd(ctx: CommandContext): number {
const skill = ctx.args[2];
if (!skill) usage();
const { options } = parseOptions(ctx.args.slice(3));
for (const key of ["title", "symptom", "cause", "fix"]) {
if (!optionString(options, key)) fail(`--${key} is required`);
}
const title = optionString(options, "title") ?? "";
const symptom = optionString(options, "symptom") ?? "";
const id = optionString(options, "id") ?? slugify(title);
const path = troubleshootingYamlPath(ctx.root, skill, id);
if (existsSync(path)) fail(`troubleshooting entry already exists: ${path}`);
const text =
`id: ${id}\n` +
`title: ${yamlQuote(title)}\n` +
`date: ${todayIso()}\n` +
"symptoms:\n" +
yamlList([symptom]) +
"\npatterns:\n" +
yamlList([symptom]) +
"\nlikely_causes:\n" +
yamlList([optionString(options, "cause") ?? ""]) +
"\nfix_steps:\n" +
yamlList([optionString(options, "fix") ?? ""]) +
"\nverification: " +
yamlQuote(optionString(options, "verify") ?? "Add the command, UI signal, or log line that proves the fix worked.") +
"\nrelated_cases:\n" +
yamlList([]) +
"\n";
writeFileSync(path, text, "utf8");
appendFileSync(legacyTroubleshootingPath(ctx.root, skill), `\n## ${id}: ${title}\n\nSee \`../troubleshooting/${id}.yaml\`.\n`, "utf8");
console.log(path);
return 0;
}