Skip to content

feat - Remove pi-mcp-adapter extension as dependencies for pi coding agent #108

Description

@tan-yong-sheng

Problem

Installing too much pi extension is sometimes hard to manage like version compatibility, etc, so we can keep uninstall the unnecessary layer to reduce maintenance overhead ...

For example, codebase-memory-mcp is also mcp tool, but using a single .ts file, we can easily translate those stdio mcp function into native tool via pi extension: https://github.com/DeusData/codebase-memory-mcp as shown below.

Proposed solution

Example code for codebase-memory-mcp pi extension that you may take reference with:

File name: ~/.pi/agent/extensions/cbmem.ts

// codebase-memory-mcp:start
// Generated by codebase-memory-mcp for pi.
// Regenerated on install/update from the MCP tool registry, so it cannot
// drift from the tools the server actually serves. Edits inside this block
// are overwritten; edit outside it, or remove the markers to take ownership.
import { spawn } from 'node:child_process';

const BIN = '/home/tys203831/.local/bin/codebase-memory-mcp';

async function call(tool, args, signal) {
  return new Promise((resolve) => {
    const child = spawn(BIN, ['cli', tool, JSON.stringify(args ?? {})], {
      stdio: ['ignore', 'pipe', 'pipe'],
      env: { ...process.env, CBM_LOG_LEVEL: 'error' },
    });
    let out = '';
    const onAbort = () => { if (!child.killed) child.kill(); };
    signal?.addEventListener('abort', onAbort, { once: true });
    child.stdout.on('data', (d) => (out += d.toString()));
    child.on('error', (e) => {
      signal?.removeEventListener('abort', onAbort);
      resolve({ error: String(e && e.message ? e.message : e) });
    });
    child.on('close', () => {
      signal?.removeEventListener('abort', onAbort);
      const lines = out.split('\n').map((l) => l.trim()).filter(Boolean);
      for (let i = lines.length - 1; i >= 0; i--) {
        try { return resolve(JSON.parse(lines[i])); } catch { /* keep scanning */ }
      }
      resolve({ error: 'no JSON response from codebase-memory-mcp' });
    });
  });
}

export default function (pi) {
  pi.registerTool({ name: 'index_repository', run: (args, ctx) => call('index_repository', args, ctx?.signal) });
  pi.registerTool({ name: 'search_graph', run: (args, ctx) => call('search_graph', args, ctx?.signal) });
  pi.registerTool({ name: 'query_graph', run: (args, ctx) => call('query_graph', args, ctx?.signal) });
  pi.registerTool({ name: 'trace_path', run: (args, ctx) => call('trace_path', args, ctx?.signal) });
  pi.registerTool({ name: 'get_code_snippet', run: (args, ctx) => call('get_code_snippet', args, ctx?.signal) });
  pi.registerTool({ name: 'get_graph_schema', run: (args, ctx) => call('get_graph_schema', args, ctx?.signal) });
  pi.registerTool({ name: 'get_architecture', run: (args, ctx) => call('get_architecture', args, ctx?.signal) });
  pi.registerTool({ name: 'search_code', run: (args, ctx) => call('search_code', args, ctx?.signal) });
  pi.registerTool({ name: 'list_projects', run: (args, ctx) => call('list_projects', args, ctx?.signal) });
  pi.registerTool({ name: 'delete_project', run: (args, ctx) => call('delete_project', args, ctx?.signal) });
  pi.registerTool({ name: 'index_status', run: (args, ctx) => call('index_status', args, ctx?.signal) });
  pi.registerTool({ name: 'check_index_coverage', run: (args, ctx) => call('check_index_coverage', args, ctx?.signal) });
  pi.registerTool({ name: 'detect_changes', run: (args, ctx) => call('detect_changes', args, ctx?.signal) });
  pi.registerTool({ name: 'manage_adr', run: (args, ctx) => call('manage_adr', args, ctx?.signal) });
  pi.registerTool({ name: 'ingest_traces', run: (args, ctx) => call('ingest_traces', args, ctx?.signal) });
}
// codebase-memory-mcp:end

This will reduce the dependencies and keep your pi extension to be as minimal ...

Alternatives considered

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions