2026-05-14 15:28:14 +00:00
|
|
|
// Package mcp wires the curated weknora tool set to an
|
|
|
|
|
// modelcontextprotocol/go-sdk server. RunStdio is the entry point invoked
|
|
|
|
|
// by `weknora mcp serve`.
|
|
|
|
|
//
|
|
|
|
|
// Design notes:
|
|
|
|
|
//
|
|
|
|
|
// - Tool surface is hand-curated rather than auto-derived from the cobra
|
|
|
|
|
// tree (which would expose auth/link/completion/destructive verbs that
|
|
|
|
|
// don't belong on an agent-callable surface).
|
2026-05-29 12:06:30 +00:00
|
|
|
// - Long-running tools (chat / session_ask) accumulate the LLM SSE
|
refactor(cli): drop aiclient package; align AGENTS.md with mainstream
Survey of 10 mainstream CLIs (gh, lark, stripe, vercel, supabase, aws,
azure, gcloud, openai/codex, github-copilot-cli) showed env-gated
per-command --help blurbs are a Stripe-only pattern; gh uses env detect
for telemetry only, and lark relies on installed agent Skills + MCP.
Our cmd/mcp/serve already covers the dominant 2025/26 path, so
internal/aiclient/ (136 LOC + 38 callsites) is net maintenance burden
without precedent.
- Drop internal/aiclient/ entirely (annotations + detect + tests)
- Remove 38 SetAgentHelp callsites + agentAwareHelpFunc / SetHelpFunc
wiring in cmd/root.go
- Migrate 4 command-level rules to standard Long help (visible to all,
not env-gated): doc upload mode mutex, kb edit at-least-one,
kb pin idempotent, search chunks channel mutex
- Rewrite AGENTS.md as a developer guide (gh-style 6 H2 / 167 lines):
audience preamble + Build / Architecture / Command Structure /
Testing / Code Style / Error Handling. Drops sections absent in
surveyed projects (Commit & PR Conventions, Who Uses This CLI)
- Clean 14 internal doc refs (ADR-N, spec §X, v0.X) in source comments
and docs that pointed at docs/superpowers/ — that directory is
local-only / uncommitted, so refs are dead for outside readers
- Drop forward-looking "once v0.2 ships" from README
2026-05-14 18:16:45 +00:00
|
|
|
// stream server-side and return a single CallToolResult - MCP spec
|
2026-05-14 15:28:14 +00:00
|
|
|
// 2025-06-18 does not define streamed tool-result content, so the
|
|
|
|
|
// accumulate-and-return pattern is the canonical path.
|
2026-05-14 15:28:14 +00:00
|
|
|
// - Handlers receive ctx for cancellation; mid-LLM-stream cancellation
|
|
|
|
|
// propagates to the SDK via context, which closes the SSE connection.
|
|
|
|
|
package mcp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-06-26 12:40:59 +00:00
|
|
|
"errors"
|
2026-05-14 15:28:14 +00:00
|
|
|
"fmt"
|
2026-06-26 12:40:59 +00:00
|
|
|
"io"
|
|
|
|
|
"strings"
|
2026-05-14 15:28:14 +00:00
|
|
|
|
|
|
|
|
mcpsdk "github.com/modelcontextprotocol/go-sdk/mcp"
|
|
|
|
|
|
|
|
|
|
"github.com/Tencent/WeKnora/cli/internal/build"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ServiceClient bundles the SDK methods the tool registry needs. *sdk.Client
|
|
|
|
|
// satisfies it; tests substitute a fake to exercise the tool handlers
|
|
|
|
|
// in-process without standing up a real WeKnora server.
|
|
|
|
|
//
|
|
|
|
|
// Embedding the full SDK Client would couple every tool test to every SDK
|
|
|
|
|
// method; declaring the narrow surface here keeps the seam tight.
|
|
|
|
|
type ServiceClient interface {
|
|
|
|
|
knowledgeBaseService
|
|
|
|
|
knowledgeService
|
|
|
|
|
chatService
|
|
|
|
|
agentService
|
2026-05-15 17:22:14 +00:00
|
|
|
chunkListService
|
2026-05-14 15:28:14 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-15 17:22:14 +00:00
|
|
|
// RunStdio constructs the MCP server, registers the curated 10 tools, and
|
2026-05-14 15:28:14 +00:00
|
|
|
// blocks reading JSON-RPC from stdin until the client disconnects or ctx
|
|
|
|
|
// is cancelled. Returns the underlying transport error (if any); the cobra
|
|
|
|
|
// RunE caller maps it through the usual cmdutil exit-code path.
|
|
|
|
|
func RunStdio(ctx context.Context, svc ServiceClient) error {
|
|
|
|
|
v, _, _ := build.Info()
|
|
|
|
|
server := mcpsdk.NewServer(
|
|
|
|
|
&mcpsdk.Implementation{
|
|
|
|
|
Name: "weknora",
|
|
|
|
|
Version: v,
|
|
|
|
|
},
|
|
|
|
|
nil,
|
|
|
|
|
)
|
|
|
|
|
registerTools(server, svc)
|
|
|
|
|
if err := server.Run(ctx, &mcpsdk.StdioTransport{}); err != nil {
|
2026-06-26 12:40:59 +00:00
|
|
|
// An MCP host shuts the server down by closing stdin (EOF) or cancelling
|
|
|
|
|
// the context — that is normal termination, not a failure, so exit 0.
|
|
|
|
|
// Only a genuine transport fault is surfaced as an error.
|
|
|
|
|
if isCleanShutdown(err) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-05-14 15:28:14 +00:00
|
|
|
return fmt.Errorf("mcp serve: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-06-26 12:40:59 +00:00
|
|
|
|
|
|
|
|
// isCleanShutdown reports whether err is the ordinary "client went away"
|
|
|
|
|
// signal: EOF on stdin or a cancelled/expired context. The go-sdk wraps the
|
|
|
|
|
// stdin EOF in a formatted string ("server is closing: EOF") that does not
|
|
|
|
|
// always unwrap to io.EOF, so we also match the message suffix as a fallback.
|
|
|
|
|
func isCleanShutdown(err error) bool {
|
|
|
|
|
if errors.Is(err, io.EOF) || errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return strings.HasSuffix(strings.TrimSpace(err.Error()), "EOF")
|
|
|
|
|
}
|