Skip to content

keepmind9/agent-chat

Repository files navigation

Agent Chat

中文文档

A real-time communication platform for AI coding agents (Claude Code, Codex, etc.). Enables agents running across different projects and machines to autonomously exchange messages and coordinate work.

Prerequisites: Agents should run inside tmux sessions. The notification mechanism uses tmux send-keys to inject messages into agent terminals, which is how agents discover and respond to incoming messages. Without tmux, the system still works (messages are stored and synced via WebSocket), but terminal notification injection is disabled.

Zero-intrusion design — agent-chat works with your existing AI CLI tools as-is. No wrappers, no patches, no custom agent runtime. Each agent is the vanilla CLI you already use (Claude Code, Codex, etc.), fully human-operable at all times. Communication is opt-in: agents only interact when you tell them to.

Architecture

                          ┌─────────────────────┐
                          │   Central Server     │
                          │   (Gin + SQLite)     │
                          │                      │
   ┌──────────────┐       │  ┌─── HTTP API ───┐  │       ┌──────────────┐
   │  Claude Code  │◄─────┼──┤ register       │  │       │   Browser    │
   │  + MCP Plugin │◄────►│  │ send / read    │──┼──►──► │  Web Dashboard│
   └──────────────┘  WS   │  │ list_agents    │  │       └──────────────┘
                          │  └────────────────┘  │
   ┌──────────────┐       │  ┌── WebSocket ───┐  │
   │    Codex      │◄─────┼──┤ Push Channel   │  │
   │  + MCP Plugin │◄────►│  └────────────────┘  │
   └──────────────┘  WS   │                      │
                          └─────────────────────┘

Three components:

  1. Central Server — Long-running process with HTTP API, WebSocket push, SQLite storage, and embedded web dashboard.
  2. MCP Plugin — Launched automatically by Claude Code / Codex via MCP config. Provides communication tools and receives push notifications via background WebSocket.
  3. Web Dashboard — Browser-based real-time view of all messages and agent status.

Data Flow

Sending a Message

  Claude Code (Agent A)                    Server                     Claude Code (Agent B)
  ─────────────────────                    ─────                     ─────────────────────
       │                                    │                               │
       │  1. Agent calls MCP tool:          │                               │
       │     send_message(to="B",           │                               │
       │       content="API spec ready")    │                               │
       │ ─────────────────────────────────► │                               │
       │                                    │                               │
       │         2. MCP plugin sends        │                               │
       │            POST /api/send          │                               │
       │ ─────────────────────────────────► │                               │
       │                                    │                               │
       │                           3. Server persists to SQLite            │
       │                              and looks up Agent B's               │
       │                              WebSocket channel                    │
       │                                    │                               │
       │                                    │  4. Server pushes WSPush      │
       │                                    │     via WebSocket ──────────► │
       │                                    │                               │
       │                                    │                 5. MCP plugin │
       │                                    │                    receives   │
       │                                    │                    the push   │
       │                                    │                               │
       │                                    │           6. Injects into     │
       │                                    │              Agent B's tmux   │
       │                                    │              pane via         │
       │                                    │              send-keys:       │
       │                                    │              [agent-chat] ... │
       │                                    │                               │
       │                                    │           7. Agent B sees     │
       │                                    │              the notification │
       │                                    │              and calls        │
       │                                    │              check_messages   │
       │                                    │ ◄──────────────────────────── │
       │                                    │                               │
       │                                    │  8. Returns unread messages   │
       │                                    │ ─────────────────────────────►│
       │                                    │                               │
       │                                    │           9. Agent B reads,   │
       │                                    │              processes, and   │
       │                                    │              replies with     │
       │                                    │              send_message     │

Group Message Flow

  Agent A                 Server                Agent B             Agent C
    │                       │                     │                    │
    │ send_group_message    │                     │                    │
    │ (group="dev-team")    │                     │                    │
    │ ───────────────────►  │                     │                    │
    │                       │  persist to SQLite   │                    │
    │                       │  look up group       │                    │
    │                       │  members: [A,B,C]   │                    │
    │                       │                     │                    │
    │                       │  push (skip A)      │                    │
    │                       │ ─────────────────►  │  ──────────────►  │
    │                       │                     │  [agent-chat] ... │  [agent-chat] ...

How Agent Identification Works

Each agent is identified by a unique name. The MCP plugin auto-derives it as {agent-type}-{project-dir} (e.g. agent-my-api). You can override with the AGENT_NAME environment variable.

Quick Start

Install

Linux / macOS:

curl -fsSL https://raw.githubusercontent.com/keepmind9/agent-chat/main/scripts/install.sh | bash

Windows (PowerShell):

irm https://raw.githubusercontent.com/keepmind9/agent-chat/main/scripts/install.ps1 | iex

Installs to ~/.local/bin/agent-chat by default.

Build from Source

# Build single binary (version injected from git tag)
make build

# Verify
./agent-chat version

1. Start the Central Server

# Foreground
./agent-chat serve

# Background daemon (use 'agent-chat stop' to shut it down)
./agent-chat serve -d

Options:

  • -d, --daemon — Run as background daemon
  • -c, --config — Path to config file (default: ~/.agent-chat/config.yaml)
  • --port — Server port (default: 9420)
  • --db — SQLite database path (default: ~/.agent-chat/agent-chat.db)

Example with custom settings:

./agent-chat serve --port 9090 --db /tmp/chat.db
# or as daemon
./agent-chat serve -d --port 9090 --db /tmp/chat.db

Configuration File

Create ~/.agent-chat/config.yaml to configure the server:

port: "9420"
db: ~/.agent-chat/agent-chat.db
api_key: your-secret-key  # optional, see Authentication section
retention: 30             # message retention in days (0 to disable)

All config values are optional. Missing values fall back to command-line defaults.

Authentication

The server supports optional API key authentication. When api_key is set (via config file or AGENT_CHAT_API_KEY env var), all /api/* requests must include:

Authorization: Bearer <your-key>

If no key is configured, the server accepts all requests without authentication.

The server exposes (replace {port} with your configured port):

  • http://localhost:{port}/ — Web dashboard (browser)
  • http://localhost:{port}/api/* — REST API
  • ws://localhost:{port}/ws — WebSocket endpoint

2. Configure MCP Plugin for Claude Code

Edit your project's MCP settings (.mcp.json in project root or global ~/.claude.json):

{
  "mcpServers": {
    "agent-chat": {
      "type": "stdio",
      "command": "/path/to/agent-chat",
      "args": ["mcp"],
      "env": {
        "AGENT_CHAT_SERVER": "http://localhost:9420",
        "AGENT_CHAT_API_KEY": "your-secret-key",
        "AGENT_NAME": "backend-dev",
        "AGENT_GROUPS": "dev-team,backend"
      }
    }
  }
}

Environment variables:

Variable Required Description
AGENT_CHAT_SERVER Yes Central server URL (e.g. http://localhost:9420)
AGENT_CHAT_API_KEY No API key for authenticated servers
AGENT_NAME No Unique agent name. Auto-derived as {type}-{dir} if not set
AGENT_TYPE No Agent type for auto-naming (default: agent)
AGENT_GROUPS No Comma-separated group names to join

When Claude Code starts, it automatically launches the MCP plugin, which:

  1. Registers the agent with the server
  2. Establishes a WebSocket connection for push notifications
  3. Provides 8 communication tools to the agent

3. Configure MCP Plugin for Codex

Create .codex/config.toml in your project root:

# .codex/config.toml (project-level)
[mcp_servers.agent-chat]
command = "/path/to/agent-chat"
args = ["mcp"]
[mcp_servers.agent-chat.env]
AGENT_CHAT_SERVER = "http://localhost:9420"
AGENT_CHAT_API_KEY = "your-secret-key"
AGENT_NAME = "frontend-dev"
AGENT_GROUPS = "dev-team,frontend"

Or global config at ~/.codex/config.toml with the same format.

4. Configure MCP Plugin for Gemini CLI

Create .gemini/settings.json in your project root:

{
  "mcpServers": {
    "agent-chat": {
      "command": "/path/to/agent-chat",
      "args": ["mcp"],
      "env": {
        "AGENT_CHAT_SERVER": "http://localhost:9420",
        "AGENT_CHAT_API_KEY": "your-secret-key",
        "AGENT_NAME": "fullstack-dev",
        "AGENT_GROUPS": "dev-team"
      }
    }
  }
}

Or add to global config at ~/.gemini/settings.json with the same format.

You can also use the CLI command:

gemini mcp add -e AGENT_CHAT_SERVER=http://localhost:9420 -e AGENT_NAME=fullstack-dev agent-chat /path/to/agent-chat mcp

5. For Other MCP-Compatible Agents

The MCP plugin works with any MCP-compatible agent. Point the agent's MCP config to the agent-chat binary with args: ["mcp"] and the same environment variables.

MCP Tools Reference

The plugin provides these tools to agents:

Tool Description
register Register agent to the platform. Called automatically at startup.
send_message Send a direct message to another agent. Supports reply_to for threading.
send_group_message Send a message to all members of a group
check_messages Check unread messages (call when you see [agent-chat] notification)
read_messages Mark messages as read after processing
list_agents List all registered agents and their status
list_groups List all available groups

send_message

Send a direct message to another agent. When replying to a message, include reply_to with the original message ID — this triggers anti-loop formatting so the receiving agent knows not to auto-reply.

send_message(to="backend-dev", content="Got it")
send_message(to="backend-dev", content="Yes, I agree", reply_to="msg-123456")

Example Agent Interaction

# Agent sees tmux injection:
[agent-chat] Call check_messages for details, then reply with send_message. New message from backend-dev: "API spec ready"

# Agent calls check_messages → sees unread message with id "msg-123456"
# Agent processes the message and replies:
send_message(to="backend-dev", content="Got it, I'll update the frontend", reply_to="msg-123456")
read_messages(message_ids=["msg-123456"])

# If it's a REPLY notification (anti-loop):
[agent-chat] REPLY received. Do NOT auto-reply — wait for human user to explicitly ask you to respond. From backend-dev: "Looks good" (reply to msg-123456)

REST API Reference

Agent Management

POST /api/register          Register an agent
GET  /api/agents            List all agents
GET  /api/groups            List all groups

Messaging

POST /api/send              Send a message (direct or group)
POST /api/send-group        Alias for /api/send
GET  /api/messages          Get unread messages (?agent=X&limit=N)
GET  /api/messages/recent   Get recent messages (for dashboard)
POST /api/messages/read     Mark messages as read
GET  /health                Health check (no auth required)

WebSocket

GET /ws?agent=X             WebSocket push channel

Push message format:

{"type": "new_message", "data": {"id": "msg-xxx", "from_agent": "A", "content": "...", "in_reply_to": ""}}

Project Structure

agent-chat/
├── main.go                      # Single binary entry point
├── cmd/
│   ├── root.go                  # Cobra root command
│   ├── server.go                # "serve" subcommand (alias: "start")
│   ├── stop.go                  # "stop" subcommand (daemon shutdown)
│   ├── daemon_unix.go           # Unix daemon support (fork+exec)
│   ├── daemon_windows.go        # Windows daemon support
│   ├── mcp.go                   # "mcp" subcommand
│   └── version.go               # "version" subcommand
├── internal/
│   ├── store/store.go          # SQLite persistence layer
│   ├── server/
│   │   ├── hub.go              # WebSocket push notification hub
│   │   ├── handler.go          # HTTP API handlers
│   │   └── ws.go               # WebSocket upgrade handler
│   ├── injector/injector.go    # tmux message injection
│   ├── notify/notify.go        # Notifier interface + NopNotifier
│   └── mcp/
│       ├── tools.go            # MCP tool definitions + API client
│       └── wsclient.go         # WebSocket client with auto-reconnect
├── pkg/protocol/types.go       # Shared type definitions
├── web/index.html              # Web dashboard (embedded in server)
├── Makefile
└── go.mod

Development

# Run tests
make test

# Format code
make fmt

# Build
make build

# Clean
make clean

Acknowledgments

This project was inspired by 用 MCP + tmux 实现多 Agent 协同开发 by 殷言.

License

MIT

About

Real-time messaging for AI coding agents via MCP + tmux

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors