@usegraft/mcp 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Anderson Joseph
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,137 @@
1
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ import { Storage } from '@usegraft/assets';
3
+ import { AnyCollection, AnyGraftFunction, FunctionActor, RateLimit } from '@usegraft/core';
4
+ import { Database, BranchScope, AuditStore, ApprovalStore } from '@usegraft/db';
5
+ import { ErrorCode } from '@usegraft/contracts';
6
+
7
+ interface GraftMcpOptions {
8
+ /** Absolute path to the content root (documents live at <contentDir>/<collection>/<slug>.mdx). */
9
+ contentDir: string;
10
+ collections: Record<string, AnyCollection>;
11
+ /**
12
+ * The Postgres content index. Omit only when serving a static project via
13
+ * `staticIndexPath` — `db` wins if both are somehow set.
14
+ */
15
+ db?: Database;
16
+ /**
17
+ * Path to a compiled static index artifact (.graft/index.db) — the
18
+ * zero-service tier. Authoring works exactly as it does on Postgres (files
19
+ * are the truth, write_content recompiles), and the Postgres-tier tools
20
+ * (functions, branches, approvals, the gated delete) answer NEEDS_DATABASE
21
+ * with the upgrade rather than being silently absent.
22
+ */
23
+ staticIndexPath?: string;
24
+ /**
25
+ * Typed functions from graft.config — enables list_functions / describe_function
26
+ * / run_function and fills describe_schema.functions. Optional so content-only
27
+ * projects still work.
28
+ */
29
+ functions?: Record<string, AnyGraftFunction>;
30
+ /** Content branch to project into. Defaults to "main". */
31
+ branchId?: string;
32
+ /**
33
+ * Resolved read scope for `branchId` (from resolveBranchHandle / resolveBranchScope)
34
+ * — what makes search_content overlay-aware: an overlay branch searches its full
35
+ * ancestor chain, so content inherited from parents is found, branch overrides
36
+ * win, and tombstones hide. When omitted, the server resolves the scope itself
37
+ * on first search (memoized per server instance, like sdk-core's per-client
38
+ * memo), so a bare branchId still searches the branch's effective content.
39
+ */
40
+ scope?: BranchScope;
41
+ /** Server identity reported to MCP clients. */
42
+ name?: string;
43
+ version?: string;
44
+ /**
45
+ * Resolve the caller for run_function (and future gated tools). Same seam as
46
+ * createFunctionsHandler / createGraftMcpHandler. Defaults to anonymous.
47
+ */
48
+ actor?: (request: Request) => FunctionActor | Promise<FunctionActor>;
49
+ /** Forwarded to createFunctionsHandler for run_function. */
50
+ approvalPolicy?: "none" | "human";
51
+ rateLimit?: RateLimit;
52
+ gitSha?: string;
53
+ /**
54
+ * Bearer token applied to run_function when the tool call passes no
55
+ * `authorization` — so the credential lives with the server (env/config),
56
+ * not in the agent's context window or MCP transcript. An explicit
57
+ * `authorization` argument still wins. `graft mcp` sets this from
58
+ * GRAFT_DEV_TOKEN; the HTTP handler forwards the caller's own header.
59
+ */
60
+ defaultAuthorization?: string;
61
+ /**
62
+ * Audit / approval stores for run_function. Defaults match createFunctionsHandler
63
+ * (db-backed). Pass `audit: false` in unit tests that do not hit a real DB.
64
+ */
65
+ audit?: AuditStore | false;
66
+ approvals?: ApprovalStore;
67
+ /**
68
+ * Registry root for list_registry / describe_item. Defaults to @usegraft/registry's
69
+ * bundled primitives — the set `graft add` installs from. Tests point it at a fixture.
70
+ */
71
+ registryRoot?: string;
72
+ /**
73
+ * Asset store for put_asset. Defaults to S3_* env config (same rules as
74
+ * `graft asset put`), resolved lazily so content-only servers never need it.
75
+ * Tests inject a fake.
76
+ */
77
+ storage?: Storage | (() => Storage | Promise<Storage>);
78
+ }
79
+ declare function createGraftMcp(options: GraftMcpOptions): McpServer;
80
+
81
+ interface GraftMcpHandlerOptions extends GraftMcpOptions {
82
+ /**
83
+ * Resolve the caller — the same @usegraft/auth `createActorResolver` seam the
84
+ * functions handler uses. A resolver that throws (TOKEN_INVALID) rejects the
85
+ * request with 401; per-tool authorization lands with function introspection.
86
+ */
87
+ actor?: (request: Request) => FunctionActor | Promise<FunctionActor>;
88
+ /**
89
+ * Reject anonymous callers with 401. Off by default (a dev server on
90
+ * localhost); turn it on for anything reachable from outside.
91
+ */
92
+ requireActor?: boolean;
93
+ }
94
+ type GraftMcpHandler = (request: Request) => Promise<Response>;
95
+ declare function createGraftMcpHandler(options: GraftMcpHandlerOptions): GraftMcpHandler;
96
+
97
+ /**
98
+ * The self-teaching error knowledge base behind the `explain_error` tool.
99
+ *
100
+ * Every ErrorCode in @usegraft/contracts has an entry: what the failure means, what
101
+ * usually causes it, and how to recover. A GraftError's `fix` is specific to one
102
+ * failure; this registry is the general lesson an agent can apply next time.
103
+ * A test asserts the registry stays in lockstep with ErrorCodes.
104
+ */
105
+
106
+ interface ErrorExplanation {
107
+ code: ErrorCode;
108
+ meaning: string;
109
+ typicalCauses: string[];
110
+ howToRecover: string;
111
+ }
112
+ declare const ERROR_KNOWLEDGE: Record<ErrorCode, ErrorExplanation>;
113
+ /** Explanation for a code, or undefined if the code is unknown. */
114
+ declare function explainCode(code: string): ErrorExplanation | undefined;
115
+
116
+ /**
117
+ * @usegraft/mcp
118
+ * MCP server: content ops + schema/function introspection + run_function +
119
+ * registry browse (list_registry / describe_item) + agent-actionable errors.
120
+ * `createGraftMcp` builds the server; `serveStdio`
121
+ * binds it to stdio for `.mcp.json` / `graft mcp`; `createGraftMcpHandler`
122
+ * serves it over Streamable HTTP as a stateless `Request → Response` handler.
123
+ * See docs/design-notes/agent-mcp.md.
124
+ */
125
+
126
+ /**
127
+ * Serve an MCP server over stdio (the transport agents' `.mcp.json` entries use).
128
+ * Resolves when the client disconnects (stdin EOF / transport close), NOT at
129
+ * connect time — callers await this for the server's whole lifetime and then
130
+ * clean up. Resolving at connect let `graft mcp` fall through to its
131
+ * process.exit right after the initialize handshake (latent P6.2 bug caught
132
+ * by the P6.5 live smoke; the example's `pnpm mcp` script masked it by never
133
+ * exiting after the await).
134
+ */
135
+ declare function serveStdio(server: McpServer): Promise<void>;
136
+
137
+ export { ERROR_KNOWLEDGE, type ErrorExplanation, type GraftMcpHandler, type GraftMcpHandlerOptions, type GraftMcpOptions, createGraftMcp, createGraftMcpHandler, explainCode, serveStdio };