| 1 |
|
package mcpsrv |
| 2 |
|
|
| 3 |
|
import ( |
| 4 |
|
"errors" |
| 5 |
|
"log/slog" |
| 6 |
|
|
| 7 |
|
"github.com/modelcontextprotocol/go-sdk/jsonrpc" |
| 8 |
|
"go.bigb.es/auxilia/scribe" |
| 9 |
|
|
| 10 |
|
"sourcecraft.dev/bigbes/sr-ht-spec/core" |
| 11 |
|
"sourcecraft.dev/bigbes/sr-ht-spec/service" |
| 12 |
|
) |
| 13 |
|
|
| 14 |
|
// The one place this surface decides what a failure *is*, which on MCP is a |
| 15 |
|
// question with two answers rather than a status code. |
| 16 |
|
// |
| 17 |
|
// - A tool result error (CallToolResult.IsError) is an answer to the agent: |
| 18 |
|
// the call was understood, executed, and the thing asked for is not there. |
| 19 |
|
// The SDK produces one out of any ordinary error a handler returns, and the |
| 20 |
|
// agent reads it as text and decides what to ask next. |
| 21 |
|
// - A protocol error (a *jsonrpc.Error returned by a handler, which the SDK |
| 22 |
|
// passes through as the JSON-RPC error of the response) says the call did |
| 23 |
|
// not produce an answer at all. The client's CallTool returns an error |
| 24 |
|
// rather than a result, which is exactly right for a git object store that |
| 25 |
|
// is down: an agent must not read "the store could not answer" as "that |
| 26 |
|
// document does not exist" and go rewrite a specification around a document |
| 27 |
|
// that is perfectly real. |
| 28 |
|
// |
| 29 |
|
// Before this file the two were one. Every error from below travelled to the |
| 30 |
|
// agent as a tool result carrying its own text, so `git object store is on |
| 31 |
|
// fire` and `no document "SPEC-0007"` were the same kind of answer, told apart |
| 32 |
|
// only by prose the agent would have had to parse. The tests pinned it: a |
| 33 |
|
// backend failure asserted that the agent was shown the words "on fire". |
| 34 |
|
// |
| 35 |
|
// The whole table is missingOrDenied plus a default, and the default is the |
| 36 |
|
// protocol arm on purpose: an unmapped error is a bug in a layer below, and |
| 37 |
|
// rendering it as a tool result would report that bug to the agent as a fact |
| 38 |
|
// about the corpus. |
| 39 |
|
|
| 40 |
|
// internalMessage is the message of every protocol error this surface returns. |
| 41 |
|
// The detail is logged, never sent: the errors below name spaces, revisions, |
| 42 |
|
// paths and git internals, and an agent holding a working token is not the |
| 43 |
|
// audience for any of it. |
| 44 |
|
const internalMessage = "internal server error" |
| 45 |
|
|
| 46 |
|
// missingOrDenied is the answer to a read that resolved to nothing. |
| 47 |
|
// |
| 48 |
|
// missing is the sentence the agent sees, and every caller builds it from the |
| 49 |
|
// arguments of the call being answered ("no space ~alice/rfcs"). That is |
| 50 |
|
// deliberate: the sentence is written from what the caller passed, so it |
| 51 |
|
// discloses nothing the caller did not already know, and it does not carry the |
| 52 |
|
// wrapped text of the error it is replacing — service/ wraps its misses with |
| 53 |
|
// what it looked up, and echoing that is how a surface eventually publishes the |
| 54 |
|
// difference between "no such space" and "not yours". |
| 55 |
|
// |
| 56 |
|
// where is the operator's half — the tool name — and appears only in the log |
| 57 |
|
// line of the protocol arm. |
| 58 |
4 |
func missingOrDenied(err error, where, missing string) error { |
| 59 |
4 |
if errors.Is(err, service.ErrNotFound) { |
| 60 |
3 |
// A tool result error: the SDK packs an ordinary error into |
| 61 |
3 |
// CallToolResult with IsError set. |
| 62 |
3 |
return errors.New(missing) |
| 63 |
3 |
} |
| 64 |
1 |
return internalError(err, where) |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
// internalError logs the cause and returns the protocol error the client sees. |
| 68 |
|
// |
| 69 |
|
// The error goes through scribe.Err, which expands a culpa chain into err.msg, |
| 70 |
|
// err.code and err.hint instead of flattening it with %v. |
| 71 |
2 |
func internalError(err error, where string) error { |
| 72 |
2 |
slog.Error("a tool call failed", "tool", where, scribe.Err(err)) |
| 73 |
2 |
return &jsonrpc.Error{Code: jsonrpc.CodeInternalError, Message: internalMessage} |
| 74 |
2 |
} |
| 75 |
|
|
| 76 |
|
// noSpace and noRevision are the two "missing" sentences the read tools pass to |
| 77 |
|
// missingOrDenied, built from the call's own arguments and nothing else. |
| 78 |
|
// |
| 79 |
|
// There is no masked-versus-absent distinction to preserve here, unlike the |
| 80 |
|
// sibling services: Gate has already established that the caller is the owner |
| 81 |
|
// or one of its agents, and this is a single-user instance whose spaces all |
| 82 |
|
// belong to that owner. A space this caller cannot see does not exist. |
| 83 |
2 |
func noSpace(ref core.SpaceRef) string { return "no space " + ref.String() } |
| 84 |
|
|
| 85 |
2 |
func noRevision(ref core.SpaceRef, rev string) string { |
| 86 |
2 |
if rev == service.ApprovedRev { |
| 87 |
1 |
return "space " + ref.String() + " has no approved revision to read" |
| 88 |
1 |
} |
| 89 |
1 |
return "no revision " + rev + " in " + ref.String() |
| 90 |
|
} |