offline-mcp 0.4.0

Model Context Protocol server exposing the Offline Protocol registry to AI agents
Documentation
//! MCP prompt templates.

use offline_registry::Registry;
use serde_json::{json, Value};

pub fn prompt_list() -> Vec<Value> {
    vec![
        json!({
            "name": "plan-app",
            "description": "Plan and scaffold an Offline Protocol app from an idea: picks a workflow, packages, and architecture, then scaffolds or integrates.",
            "arguments": [
                {"name": "description", "description": "What you want to build", "required": true},
                {"name": "platform", "description": "react-native | nextjs | react-vite (optional)", "required": false},
                {"name": "name", "description": "Project name to scaffold (optional — if omitted, stop after planning)", "required": false}
            ]
        }),
        json!({
            "name": "build-p2p-app",
            "description": "Plan and scaffold any custom offline P2P app (games, file share, polls, tools) with a custom wire protocol over mesh-sdk.",
            "arguments": [
                {"name": "description", "description": "What P2P app to build", "required": true},
                {"name": "platform", "description": "react-native (default for mesh P2P)", "required": false},
                {"name": "name", "description": "Project name to scaffold (optional)", "required": false}
            ]
        }),
        json!({
            "name": "integrate-package",
            "description": "Integrate one Offline Protocol package into an existing app using integrate_packages + skill documents.",
            "arguments": [
                {"name": "package", "description": "Package name, e.g. @offline-protocol/mesh-sdk", "required": true},
                {"name": "dir", "description": "Project directory (default: .)", "required": false}
            ]
        }),
    ]
}

pub fn get_prompt(registry: &Registry, name: &str, args: &Value) -> Result<Value, String> {
    match name {
        "plan-app" => {
            let description = args
                .get("description")
                .and_then(Value::as_str)
                .ok_or("missing required argument `description`")?;
            let platform = args.get("platform").and_then(Value::as_str);
            let project_name = args.get("name").and_then(Value::as_str);
            let platform_clause = platform
                .map(|p| format!(" Target platform: {p}."))
                .unwrap_or_default();
            let scaffold_clause = project_name
                .map(|n| {
                    format!(
                        "\n5. Call `scaffold_project` with name=\"{n}\" (and the same prompt/platform) \
to create the project on disk, then read every skill in `nextActions` with `get_skill` \
before writing Offline Protocol SDK code."
                    )
                })
                .unwrap_or_else(|| {
                    "\n5. If the user wants a new repo, call `preview_scaffold` then `scaffold_project`. \
If they have an existing app, use `integrate_packages` + `init_project`."
                        .to_string()
                });
            let text = format!(
                "I want to build: {description}.{platform_clause}\n\n\
Use the offline-protocol MCP tools to plan and integrate Offline Protocol components:\n\
1. Call `generate_architecture` with my description to get the recommended \
workflow, packages, architecture documents, and `nextActions`.\n\
2. Follow `nextActions` in order — do not invent package names or SDK APIs.\n\
3. Always read listed skills with `get_skill` before writing Offline Protocol code.\n\
4. Prefer `scaffold_project` (greenfield) or `integrate_packages` + `init_project` \
(brownfield) over hand-rolling installs or project layout.{scaffold_clause}\n\
6. Summarize: screens, providers/services, protocol events, licensing notices, \
and the exact post-generate commands from the scaffold result."
            );
            Ok(prompt_result("Plan an Offline Protocol app", &text))
        }
        "build-p2p-app" => {
            let description = args
                .get("description")
                .and_then(Value::as_str)
                .ok_or("missing required argument `description`")?;
            let platform = args
                .get("platform")
                .and_then(Value::as_str)
                .unwrap_or("react-native");
            let project_name = args.get("name").and_then(Value::as_str);
            let scaffold_clause = project_name
                .map(|n| {
                    format!(
                        "\n7. Call `scaffold_project` with name=\"{n}\", platform=\"{platform}\", \
and the same prompt to create the project.\n\
8. Read skills `p2p-app-architecture`, `p2p-discovery-patterns`, and `p2p-sync-patterns` \
with `get_skill` before writing code.\n\
9. Run `npm test`, then implement app logic in `src/domain/` and extend `src/wire.ts`."
                    )
                })
                .unwrap_or_else(|| {
                    format!(
                        "\n7. Call `preview_scaffold` then `scaffold_project` with platform=\"{platform}\".\n\
8. Read all P2P skills with `get_skill` before writing code."
                    )
                });
            let text = format!(
                "Build a custom offline P2P app: {description}.\n\n\
Use the offline-protocol MCP tools:\n\
1. Call `generate_architecture` — expect workflow `custom-p2p-app` and a P2P profile in architecture.md.\n\
2. Follow `nextActions` — do not invent package names or SDK APIs.\n\
3. The scaffold uses a four-layer layout: domain/ → wire.ts → session.ts → mesh.ts.\n\
4. Pick the sync archetype and discovery pattern from the P2P profile and skills.\n\
5. Keep mesh-sdk imports only in `src/mesh.ts`. Use the scaffold's mesh join/discovery as-is until `npm test` passes.\n\
6. Check architecture.md **App scaffold focus** (`p2p_app_kind`): generic shell, a named game, or the default tic-tac-toe sample when the prompt only said \"game\" without a title. Implement your game in `src/domain/`, `src/wire.ts`, `src/session.ts`, and `App.tsx` — not in mesh.ts.{scaffold_clause}\n\
10. Summarize: wire message types, session phases, discovery mode, debug log tag, test command, and post-generate commands."
            );
            Ok(prompt_result("Build a custom P2P app", &text))
        }
        "integrate-package" => {
            let package = args
                .get("package")
                .and_then(Value::as_str)
                .ok_or("missing required argument `package`")?;
            let dir = args.get("dir").and_then(Value::as_str).unwrap_or(".");
            let pkg = registry
                .package(package)
                .ok_or_else(|| format!("package `{package}` not found"))?;
            let skills = pkg.skills.join("`, `");
            let text = format!(
                "Integrate `{package}` v{} into the app at `{dir}`.\n\n\
Before writing code:\n\
1. Call `integrate_packages` with packages=[\"{package}\"] and dir=\"{dir}\".\n\
2. Call `init_project` on the same dir if `.mcp.json` / AGENTS.md are missing.\n\
3. Read skills with `get_skill` (`{skills}`) and check `get_package` for peers \
and notes. Wire providers following the skill exactly — do not invent APIs.\n\
4. List protocol events or hooks the app must handle and flag licensing notices.",
                pkg.version
            );
            Ok(prompt_result(&format!("Integrate {package}"), &text))
        }
        other => Err(format!("unknown prompt `{other}`")),
    }
}

fn prompt_result(description: &str, text: &str) -> Value {
    json!({
        "description": description,
        "messages": [
            {"role": "user", "content": {"type": "text", "text": text}}
        ]
    })
}