Files
LangBot/skills/src/commands/skill.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

129 lines
5.2 KiB
TypeScript

import { 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 { loadFixtureItems } from "../fixtures.ts";
import { boolValue, getSkill, globMarkdownRefs, listValue, loadSkills, loadStructuredItems, scalar, skillsRoot } from "../fs.ts";
export function commandList(ctx: CommandContext): number {
for (const skill of loadSkills(ctx.root)) {
console.log(`${skill.directory}\t${skill.name}\t${skill.description}`);
}
return 0;
}
function buildIndexData(root: string): Record<string, unknown> {
const caseSummary = (item: ReturnType<typeof loadStructuredItems>[number]) => ({
id: scalar(item.fields, "id"),
title: scalar(item.fields, "title"),
mode: scalar(item.fields, "mode"),
area: scalar(item.fields, "area"),
type: scalar(item.fields, "type"),
priority: scalar(item.fields, "priority"),
risk: scalar(item.fields, "risk"),
ci_eligible: boolValue(item.fields, "ci_eligible") ?? false,
tags: listValue(item.fields, "tags"),
automation: scalar(item.fields, "automation"),
setup_automation: listValue(item.fields, "setup_automation"),
setup_provides_env: listValue(item.fields, "setup_provides_env"),
evidence_required: listValue(item.fields, "evidence_required"),
});
const troubleshootingSummary = (item: ReturnType<typeof loadStructuredItems>[number]) => ({
id: scalar(item.fields, "id"),
title: scalar(item.fields, "title"),
category: scalar(item.fields, "category") || "product",
related_cases: listValue(item.fields, "related_cases"),
});
const suiteSummary = (item: ReturnType<typeof loadStructuredItems>[number]) => ({
id: scalar(item.fields, "id"),
title: scalar(item.fields, "title"),
description: scalar(item.fields, "description"),
type: scalar(item.fields, "type"),
priority: scalar(item.fields, "priority"),
tags: listValue(item.fields, "tags"),
cases: listValue(item.fields, "cases"),
});
return {
generated_by: "lbs",
skills: loadSkills(root).map((skill) => ({
directory: skill.directory,
name: skill.name,
description: skill.description,
references: globMarkdownRefs(skill.path),
cases: loadStructuredItems(root, "cases", skill.directory).map((item) => scalar(item.fields, "id")),
case_summaries: loadStructuredItems(root, "cases", skill.directory).map(caseSummary),
suites: loadStructuredItems(root, "suites", skill.directory).map((item) => scalar(item.fields, "id")),
suite_summaries: loadStructuredItems(root, "suites", skill.directory).map(suiteSummary),
fixtures: loadFixtureItems(root, skill.directory).items.map((item) => ({
id: item.id,
title: item.title,
kind: item.kind,
path: item.path,
related_cases: item.related_cases,
})),
troubleshooting: loadStructuredItems(root, "troubleshooting", skill.directory).map((item) => scalar(item.fields, "id")),
troubleshooting_summaries: loadStructuredItems(root, "troubleshooting", skill.directory).map(troubleshootingSummary),
})),
};
}
export function commandIndex(ctx: CommandContext): number {
const { options } = parseOptions(ctx.args.slice(1));
const data = buildIndexData(ctx.root);
const out = join(ctx.root, "skills.index.json");
const content = `${JSON.stringify(data, null, 2)}\n`;
if (options.check === true) {
if (!existsSync(out)) {
console.error(`ERROR: missing index: ${out}`);
return 1;
}
if (readFileSync(out, "utf8") !== content) {
console.error(`ERROR: index is stale: ${out}`);
return 1;
}
console.log(`OK ${out}`);
return 0;
}
writeFileSync(out, content, "utf8");
console.log(out);
return 0;
}
export function commandNewSkill(ctx: CommandContext): number {
const { positional, options } = parseOptions(ctx.args.slice(1));
const name = positional[0];
if (!name) usage();
const skillDir = join(skillsRoot(ctx.root), name);
const skillMd = join(skillDir, "SKILL.md");
if (existsSync(skillMd)) fail(`skill already exists: ${skillDir}`);
mkdirSync(skillDir, { recursive: true });
const description = optionString(options, "description") ?? `Use when working with ${name}.`;
const text =
`---\nname: ${name}\ndescription: ${description}\n---\n\n` +
`# ${name}\n\n` +
"Add concise routing and workflow instructions here.\n";
writeFileSync(skillMd, text, "utf8");
console.log(skillMd);
return 0;
}
export function commandNewRef(ctx: CommandContext): number {
const skill = ctx.args[1];
const rawName = ctx.args[2];
if (!skill || !rawName) usage();
const skillDir = getSkill(ctx.root, skill).path;
const refsDir = join(skillDir, "references");
mkdirSync(refsDir, { recursive: true });
const name = rawName.endsWith(".md") ? rawName : `${rawName}.md`;
const refPath = join(refsDir, name);
if (existsSync(refPath)) fail(`reference already exists: ${refPath}`);
const title = name.replace(/\.md$/, "").replace(/-/g, " ").replace(/\b\w/g, (char) => char.toUpperCase());
writeFileSync(refPath, `# ${title}\n\nAdd concise reusable instructions here.\n`, "utf8");
console.log(refPath);
return 0;
}