feat(cli): ping-peer CLI + MCP ping_peer for peer messaging (#1195)

* feat(cli): add ping-peer CLI and MCP ping_peer for peer messaging

Promote resume-if-inactive + wait-active + POST message into a first-class
CLI command and session MCP tool so agents stop reinventing JWT+curl.

Fixes #1194

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): do not auto-approve MCP ping_peer

Cross-session messaging can resume a peer and inject a prompt, so keep
permission-mode gating (Codex PR review Major on #1195).

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): require approval for ping_peer in read-only mode

Read-only auto-approve treated non-write names as safe; ping_peer can still
resume a peer and inject prompts, so gate it like a write tool.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): keep ping_peer out of Claude --allowedTools

toolNames still registers the MCP tool, but Claude auto-allow must not
pre-approve cross-session resume+inject without a permission prompt.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(cli): re-check session active before ping-peer send

List/get can race; POST /messages still 409s if the target flips inactive
before send. Resume+wait again (and re-gate pi) immediately before POST.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
HeavyGee
2026-07-28 08:36:56 +08:00
committed by GitHub
co-authored by Cursor
parent 84cd9aa3b1
commit e2631a553a
20 changed files with 1241 additions and 21 deletions
+4 -3
View File
@@ -53,12 +53,13 @@ describe('runHappyMcpStdioBridge tool forwarding', () => {
'--url',
'http://127.0.0.1:43006',
'--tools',
'change_title,display_image,skill_lookup'
'change_title,display_image,ping_peer,skill_lookup'
])
expect([...harness.tools.keys()]).toEqual([
'change_title',
'display_image',
'ping_peer',
'skill_lookup'
])
@@ -79,9 +80,9 @@ describe('runHappyMcpStdioBridge tool forwarding', () => {
'--url',
'http://127.0.0.1:43006',
'--tools',
'change_title,display_image'
'change_title,display_image,ping_peer'
])
expect([...harness.tools.keys()]).toEqual(['change_title', 'display_image'])
expect([...harness.tools.keys()]).toEqual(['change_title', 'display_image', 'ping_peer'])
})
})
+34 -2
View File
@@ -1,7 +1,7 @@
/**
* HAPI MCP STDIO Bridge
*
* Minimal STDIO MCP server exposing HAPI tools such as `change_title` and `display_image`.
* Minimal STDIO MCP server exposing HAPI tools such as `change_title`, `display_image`, and `ping_peer`.
* On invocation it forwards the tool call to an existing HAPI HTTP MCP server
* using the StreamableHTTPClientTransport.
*
@@ -17,7 +17,7 @@ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { z } from 'zod';
const DEFAULT_TOOL_NAMES = ['change_title', 'display_image'];
const DEFAULT_TOOL_NAMES = ['change_title', 'display_image', 'ping_peer'];
function parseArgs(argv: string[]): { url: string | null; toolNames: Set<string> } {
let url: string | null = null;
@@ -133,6 +133,38 @@ export async function runHappyMcpStdioBridge(argv: string[]): Promise<void> {
);
}
const pingPeerInputSchema: z.ZodTypeAny = z.object({
sessionIdPrefix: z.string().trim().min(1).describe(
'Target HAPI session id or unique id prefix (another session - not this chat)'
),
message: z.string().min(1).describe('Message text to deliver to the target session'),
});
if (toolNames.has('ping_peer')) {
server.registerTool<any, any>(
'ping_peer',
{
description: 'Send a message to another HAPI session (peer handoff / nudge). Resolves by session id prefix, resumes if inactive, then POSTs the message on the same hub/namespace. Prefer this (or `hapi ping-peer`) over reinventing JWT+curl. Targets another session - not the current chat.',
title: 'Ping Peer Session',
inputSchema: pingPeerInputSchema,
},
async (args: Record<string, unknown>) => {
try {
const client = await ensureHttpClient();
const response = await client.callTool({ name: 'ping_peer', arguments: args });
return response as any;
} catch (error) {
return {
content: [
{ type: 'text' as const, text: `Failed to ping peer: ${error instanceof Error ? error.message : String(error)}` },
],
isError: true,
};
}
}
);
}
const skillLookupInputSchema: z.ZodTypeAny = z.object({
name: z.string().trim().min(1).max(128).describe('Exact skill name shown by HAPI skill autocomplete'),
});
@@ -14,8 +14,8 @@ vi.mock('@/claude/utils/startHappyServer', () => ({
return {
url: 'http://127.0.0.1:43006/',
toolNames: options.skillLookup
? ['change_title', 'display_image', 'skill_lookup']
: ['change_title', 'display_image'],
? ['change_title', 'display_image', 'ping_peer', 'skill_lookup']
: ['change_title', 'display_image', 'ping_peer'],
stop: vi.fn()
}
})
@@ -71,7 +71,7 @@ describe('buildHapiMcpBridge skill lookup config', () => {
'--url',
'http://127.0.0.1:43006/',
'--tools',
'change_title,display_image,skill_lookup'
'change_title,display_image,ping_peer,skill_lookup'
])
expect(bridge.mcpServers.hapi.tools).toEqual({
change_title: { approval_mode: 'approve' },
@@ -82,7 +82,7 @@ describe('buildHapiMcpBridge skill lookup config', () => {
it('does not expose skill_lookup for native-skill bridge callers', async () => {
const bridge = await buildHapiMcpBridge(createClient())
expect(harness.cliArgs.at(-1)).toBe('change_title,display_image')
expect(harness.cliArgs.at(-1)).toBe('change_title,display_image,ping_peer')
expect(bridge.mcpServers.hapi.tools).toEqual({
change_title: { approval_mode: 'approve' }
})
@@ -95,6 +95,8 @@ export async function buildHapiMcpBridge(
approval_mode: 'approve'
};
}
// ping_peer is registered on the HTTP MCP server / stdio bridge, but is not
// auto-approved: it targets another session (resume + inject message).
if (options.skillLookup) {
tools.skill_lookup = {
approval_mode: 'approve'
+1
View File
@@ -19,6 +19,7 @@ export const TITLE_INSTRUCTION = trimIdent(`
Do not rename the chat for routine progress, substeps, implementation details, or a slightly better wording.
Rename only when the user's primary objective changes substantially and the existing title would be misleading.
When you create or find a local image file that the user should see, call functions.hapi__display_image with the image path. If that exact tool name is unavailable, use an equivalent alias such as hapi__display_image, mcp__hapi__display_image, or hapi_display_image.
To message another HAPI session (peer handoff / nudge), call functions.hapi__ping_peer with sessionIdPrefix and message - do not reinvent JWT+curl. Shell fallback: hapi ping-peer <prefix> <message>.
`);
/**