logicsrc/packages/logicsrc-mcp/src/server.ts
Anthony Ettinger ade33efd17
Machine API keys: headless teams pull for deploy boxes and CI (#230)
A person's lsk_ key acts as the person, with one identity key per user. Logging
a fresh box in with `login --token` uploaded the box's new keypair as the
person's identity and replaced the real one. Machine keys fix that: a key made
for a box, scoped to one team (and optionally named vaults), read-only unless
--read-write, optionally expiring, with its OWN identity key registered on the
key row. A vault key reaches it as a grant sealed to that public key
(credshare_key_grants), never through the person.

Schema (005_machine_keys, SQLite + Postgres): api_keys gains kind, team_id,
vault_scope, read_only (default 0, so existing person keys keep writing),
expires_at, public_key; new credshare_key_grants; credshare_audit.actor_key_id.

Server: each credshare route declares what a machine key may do (read / write /
register); the rest are people-only. Machine keys see only their team and
scoped vaults, read their grant from credshare_key_grants (audited as
vault:read with the key id), are refused every write when read-only, and cannot
mint/list/revoke keys, read /api/me or touch the personal vault. Expired and
revoked keys are 401. New GET|POST /api/keys, DELETE /api/keys/:id, and
GET|POST|DELETE /api/credshare/vaults/:id/key-grants. A re-key re-seals the
machine grants it is given and drops the rest.

CLI 0.8.0: `logicsrc keys create|list|revoke` (--team, --vault, --read-only /
--read-write, --expires, --out 0600); LOGICSRC_API_KEY and
`login --api-key` with a per-key identity in keys/<prefix>.json (0600),
generated and registered on first use; `login --token` with a machine key takes
that path instead of replacing the person's key; `teams grant … --key <name>`;
`credentials rotate` re-seals machine keys.

PWA: settings creates machine keys (team, vaults, read-only, expiry); a vault
page grants or revokes a machine key in the browser after unlock.

MCP: list_api_keys / create_api_key / revoke_api_key over /api/keys with a
Bearer LOGICSRC_API_KEY.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 23:08:32 -07:00

295 lines
10 KiB
TypeScript

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { assertSchemaKind, parseDocument, schemas, validate, type SchemaKind } from "@logicsrc/validators";
import { registerOpenOntology } from "./openontology.js";
import { registerOpenPrd } from "./openprd.js";
import { registerApiKeys, type ApiKeyToolsOptions } from "./apikeys.js";
const docs = {
"communication-accounts": `LogicSRC Communication Accounts defines shared contracts for connecting social and email identities, granting scoped human/agent/plugin access, evaluating policy gates, brokering credentials, and auditing every account action without exposing raw secrets.`,
positioning: `LogicSRC is an open standards initiative for human and AI agent coordination, maintained by Profullstack, Inc.
CommandBoard.run is a hosted product by Profullstack, Inc., built on LogicSRC. LogicSRC defines identity, boards, posts, tasks, bounties, agents, agent runs, permissions, payments, escrow, reputation, events, webhooks, CLI commands, API schemas, and plugin contracts.`,
roadmap: `LogicSRC v1.0 focuses on schemas, validation tooling, plugin manifests, CLI/TUI conventions, event streams, agent profiles, permissions, and reference implementations.`,
primitives: `Core LogicSRC primitives: users, DIDs, OAuth accounts, profiles, organizations, boards, posts, threads, comments, tasks, bids, submissions, agents, agent runs, payments, escrows, wallets, reputation events, files, API keys, permissions, audit logs, webhooks, schema versions, and plugin audit logs.`
} as const;
const schemaKinds = Object.keys(schemas) as SchemaKind[];
export function createLogicSrcMcpServer(options: { apiKeys?: ApiKeyToolsOptions } = {}) {
const server = new McpServer(
{
name: "@profullstack/logicsrc-mcp",
version: "0.1.0"
},
{
capabilities: {
resources: {},
tools: {},
prompts: {}
},
instructions: "Use this server for LogicSRC standards, schema resources, validation, draft object generation, OpenOntology knowledge (entities, claims, provenance, governed change sets), and OpenPRD product requirements documents, and LogicSRC API keys (list/create/revoke over the credentials app, with LOGICSRC_API_KEY). Treat CommandBoard.run as a reference implementation, not the standards identity. Ontology and PRD write tools propose; they never apply."
}
);
for (const [name, text] of Object.entries(docs)) {
const uri = `logicsrc://docs/${name}`;
server.registerResource(
`logicsrc-${name}`,
uri,
{
title: `LogicSRC ${titleCase(name)}`,
description: `LogicSRC ${name} reference text.`,
mimeType: "text/markdown"
},
async () => ({ contents: [{ uri, mimeType: "text/markdown", text }] })
);
}
for (const kind of schemaKinds) {
const uri = `logicsrc://schemas/${kind}`;
server.registerResource(
`logicsrc-schema-${kind}`,
uri,
{
title: `LogicSRC ${kind} schema`,
description: `JSON Schema for LogicSRC ${kind} documents.`,
mimeType: "application/schema+json"
},
async () => ({
contents: [
{
uri,
mimeType: "application/schema+json",
text: JSON.stringify(schemas[kind], null, 2)
}
]
})
);
}
server.registerTool(
"list_schema_kinds",
{
title: "List LogicSRC Schema Kinds",
description: "Lists the LogicSRC schema kinds exposed by this standards server.",
annotations: { readOnlyHint: true, openWorldHint: false }
},
async () => textResult(JSON.stringify({ schemaKinds }, null, 2))
);
server.registerTool(
"validate_document",
{
title: "Validate LogicSRC Document",
description: "Validates a JSON or YAML document against a LogicSRC schema kind.",
inputSchema: {
kind: z.enum(["account-audit-event", "account-grant", "account-provider", "agent", "connected-account", "email-message", "event", "plugin", "run", "social-post", "task"]),
document: z.string().describe("JSON or YAML document text."),
fileName: z.string().optional().describe("Optional file name used to select JSON parsing when it ends with .json.")
},
annotations: { readOnlyHint: true, openWorldHint: false }
},
async ({ kind, document, fileName }) => {
const parsed = parseDocument(document, fileName ?? "document.yaml");
const result = validate(assertSchemaKind(kind), parsed);
return textResult(JSON.stringify(result.ok ? { ok: true, kind: result.kind } : { ok: false, kind: result.kind, errors: result.errors }, null, 2));
}
);
server.registerTool(
"example_document",
{
title: "Generate Example LogicSRC Document",
description: "Returns a minimal example document for a LogicSRC schema kind.",
inputSchema: {
kind: z.enum(["account-audit-event", "account-grant", "account-provider", "agent", "connected-account", "email-message", "event", "plugin", "run", "social-post", "task"])
},
annotations: { readOnlyHint: true, openWorldHint: false }
},
async ({ kind }) => textResult(JSON.stringify(exampleFor(kind), null, 2))
);
server.registerPrompt(
"create-valid-task",
{
title: "Create Valid LogicSRC Task",
description: "Prompt template for turning a workflow request into a valid LogicSRC task.",
argsSchema: {
request: z.string().describe("Human description of the desired task.")
}
},
async ({ request }) => ({
messages: [
{
role: "user",
content: {
type: "text",
text: `Create a valid LogicSRC task JSON document for this request. Use logicsrc://schemas/task and keep it minimal unless details are required.\n\nRequest:\n${request}`
}
}
]
})
);
server.registerPrompt(
"review-plugin-manifest",
{
title: "Review LogicSRC Plugin Manifest",
description: "Prompt template for reviewing a plugin manifest against the LogicSRC plugin schema.",
argsSchema: {
manifest: z.string().describe("Plugin manifest JSON or YAML.")
}
},
async ({ manifest }) => ({
messages: [
{
role: "user",
content: {
type: "text",
text: `Review this LogicSRC plugin manifest against logicsrc://schemas/plugin. Identify schema issues, security concerns, missing permissions, and unclear capabilities.\n\nManifest:\n${manifest}`
}
}
]
})
);
registerOpenOntology(server);
registerOpenPrd(server);
registerApiKeys(server, options.apiKeys);
return server;
}
function textResult(text: string) {
return { content: [{ type: "text" as const, text }] };
}
function titleCase(value: string) {
return value.replace(/(^|-)([a-z])/g, (_match, prefix: string, letter: string) => `${prefix ? " " : ""}${letter.toUpperCase()}`);
}
function exampleFor(kind: SchemaKind) {
switch (kind) {
case "account-audit-event":
return {
id: "acct_audit_123",
provider: "gmail",
kind: "email",
principal: { type: "agent", id: "marketing-agent" },
action: "email:send",
decision: "approval_required",
riskScore: 0.35,
requestPreview: { draft_id: "draft_123" },
resultPreview: {},
createdAt: new Date(0).toISOString()
};
case "account-grant":
return {
id: "grant_123",
accountId: "account_123",
principal: { type: "agent", id: "marketing-agent" },
permissions: ["email:headers:read", "email:draft"],
policy: [],
createdAt: new Date(0).toISOString()
};
case "account-provider":
return {
id: "gmail",
name: "Gmail",
kind: "email",
authMethods: ["oauth2"],
capabilities: ["email.headers.read", "email.search"]
};
case "agent":
return {
type: "logicsrc.agent",
version: "0.1",
agent_did: "qa-agent-01.coinpay",
name: "QA Agent",
capabilities: ["browser.qa", "report.write"],
status: "active"
};
case "connected-account":
return {
id: "account_123",
ownerUserId: "user_123",
kind: "email",
provider: "gmail",
displayName: "Founder Inbox",
email: "founder@example.com",
status: "connected",
scopes: ["gmail.metadata"],
capabilities: ["email.headers.read", "email.search"],
credentialRef: "cred://gmail/account_123",
metadata: {},
createdAt: new Date(0).toISOString(),
updatedAt: new Date(0).toISOString()
};
case "email-message":
return {
id: "email_msg_123",
providerMessageId: "provider_msg_123",
subject: "Hello",
toAddresses: ["founder@example.com"],
ccAddresses: [],
labels: ["inbox"],
hasAttachments: false
};
case "event":
return {
type: "logicsrc.event",
version: "0.1",
event_id: "evt_123",
event_type: "task.created",
resource_type: "task",
resource_id: "task_123",
actor_did: "anthony.coinpay",
created_at: new Date(0).toISOString()
};
case "plugin":
return {
type: "logicsrc.plugin",
version: "0.1",
id: "example-plugin",
name: "Example Plugin",
description: "Example LogicSRC plugin manifest.",
capabilities: ["tasks.read"],
permissions: ["tasks:read"]
};
case "run":
return {
type: "logicsrc.run",
version: "0.1",
run_id: "run_123",
task_id: "task_123",
agent_did: "qa-agent-01.coinpay",
status: "completed",
started_at: new Date(0).toISOString()
};
case "social-post":
return {
id: "social_post_123",
accountId: "account_123",
status: "draft",
text: "Launching today",
media: [],
metadata: {},
createdAt: new Date(0).toISOString(),
updatedAt: new Date(0).toISOString()
};
case "task":
return {
type: "logicsrc.task",
version: "0.1",
title: "Test checkout flow",
description: "Verify checkout flow across desktop and mobile.",
board: "/qa",
creator_did: "anthony.coinpay",
status: "open",
budget: { amount: 25, currency: "USDC" },
agent_allowed: true,
human_allowed: true
};
}
}