Add hooks and MCP as first-class components; expand docs

Promotes the two remaining "deliberately not in scope" components from
the harness model to first-class subsystems, and overhauls the docs.

Hooks (runtime/types.go, runtime/runtime.go, runtime/hooks_test.go):
- New runtime.LifecycleHooks struct on LoopConfig.Hooks with five
  Go callbacks: OnUserPromptSubmit (may rewrite prompt),
  OnSessionStart, BeforeToolUse (gating with same shape as
  PermissionChecker), AfterToolUse (observe), OnStop (with reason:
  completed/max_turns/error/aborted).
- Wired into Run, dispatchTool, executeToolKickoff at all 8 exit
  points + both tool-execution paths.
- 8 tests covering each hook firing.

MCP (tools/mcp/, runtime/builder.go, runtime/types.go):
- New tools/mcp package using the official
  github.com/modelcontextprotocol/go-sdk v1.5.0. Stdio and
  Streamable HTTP transports.
- ServerConfig now exposes Headers + HTTPClient for HTTP auth
  (static bearer, OAuth via oauth2.NewClient, mTLS, custom
  RoundTripper). Header injection clones requests so SDK retries
  see clean params.
- First-class AgentSpec.MCPServers — BuildRuntime connects each
  server, registers adapter tools (mcp__<server>__<tool>), stashes
  clients on Runtime.
- New Runtime.Close() releases sessions; idempotent.
- 14 tests including end-to-end header propagation through the SDK
  via httptest.Server.

Examples:
- support-agent now demonstrates Skills + Permission + LifecycleHooks
  together. Audit log via AfterToolUse + OnStop, JSONL output.
- defer rt.Close() added to all four examples (no-op without MCP,
  idiomatic).

Docs:
- README expanded: Why harness, Requirements, Install, Examples
  table, Configuration (env vars), Non-goals, Testing, expanded
  Status. Quick Start fixed to use real BuildRuntime API.
- developer.md mental-model diagram replaced with two correct ones:
  composition (how a Runtime is wired) and per-Run sequence (what
  rt.Run actually does, including hook fan-out and the tool batch
  loop).
- developer.md gains Lifecycle hooks + MCP servers subsections with
  full snippets, including Streamable HTTP auth and OAuth recipes
  (clientcredentials + authcode flows via golang.org/x/oauth2).
- LICENSE file added (MIT).

All 18 packages build and test green.
This commit is contained in:
sausheong committed 2026-05-08 13:34:00 +08:00
1 parent 4b2f645f16
commit 76c03019c8
19 files changed
+1926 -59

No files matched your search

+23
View File
@@ -1,6 +1,8 @@
package runtime
import (
"context"
"fmt"
"log/slog"
"github.com/sausheong/harness/compaction"
@@ -8,6 +10,7 @@ import (
"github.com/sausheong/harness/session"
"github.com/sausheong/harness/tokens"
"github.com/sausheong/harness/tool"
"github.com/sausheong/harness/tools/mcp"
)
// RuntimeDeps holds the long-lived dependencies that every Runtime in this
@@ -87,6 +90,7 @@ func BuildRuntime(deps RuntimeDeps, inputs RuntimeInputs, spec AgentSpec) (*Runt
// *tool.Registry rather than widening tool.Executor — production
// callers always pass *tool.Registry; test paths that don't won't
// get load tools registered (which is fine for them).
var mcpClients []*mcp.Client
if reg, ok := inputs.Tools.(*tool.Registry); ok {
if deps.Skills != nil {
skills := deps.Skills
@@ -104,6 +108,24 @@ func BuildRuntime(deps RuntimeDeps, inputs RuntimeInputs, spec AgentSpec) (*Runt
},
})
}
// Connect declared MCP servers and register their adapter tools.
// On any failure, close everything connected so far before
// returning — a partially-built Runtime would leak processes.
for _, srv := range spec.MCPServers {
cli, err := mcp.Connect(context.Background(), srv)
if err != nil {
for _, c := range mcpClients {
_ = c.Close()
}
return nil, fmt.Errorf("mcp server %q: %w", srv.Name, err)
}
for _, t := range cli.Tools() {
reg.Register(t)
}
mcpClients = append(mcpClients, cli)
}
} else if len(spec.MCPServers) > 0 {
return nil, fmt.Errorf("AgentSpec.MCPServers requires inputs.Tools to be *tool.Registry")
}
// Pre-compute the static portion of the system prompt so the per-turn
@@ -167,6 +189,7 @@ func BuildRuntime(deps RuntimeDeps, inputs RuntimeInputs, spec AgentSpec) (*Runt
AgentLoop: deps.AgentLoop,
StaticSystemPrompt: staticPrompt,
CalibratorStore: deps.CalibratorStore,
mcpClients: mcpClients,
}
// Seed the calibrator from prior (ratio, count) for this session so a