@neon/tools 0.0.0 → 0.2.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/dist/mcp.js ADDED
@@ -0,0 +1,60 @@
1
+ //#region src/mcp.ts
2
+ const errorMessage = (error) => error instanceof Error ? error.message : String(error);
3
+ const isRecord = (value) => typeof value === "object" && value !== null;
4
+ const authInfoFrom = (context) => {
5
+ if (!isRecord(context)) return;
6
+ if (isRecord(context.http) && "authInfo" in context.http) return context.http.authInfo;
7
+ if ("authInfo" in context) return context.authInfo;
8
+ };
9
+ const bearerCredentialFromMcpContext = (context) => {
10
+ const authInfo = authInfoFrom(context);
11
+ if (authInfo === void 0) return;
12
+ if (!isRecord(authInfo) || typeof authInfo.token !== "string" || authInfo.token.length === 0) throw new TypeError("A Neon API key or OAuth access token is required");
13
+ return authInfo.token;
14
+ };
15
+ const signalFrom = (context) => {
16
+ if (!isRecord(context)) return;
17
+ if (context.signal instanceof AbortSignal) return context.signal;
18
+ if (isRecord(context.mcpReq) && context.mcpReq.signal instanceof AbortSignal) return context.mcpReq.signal;
19
+ };
20
+ const registerNeonTools = (server, tools) => {
21
+ if (typeof server.registerTool !== "function") throw new TypeError("Expected an MCP server with registerTool().");
22
+ for (const tool of Object.values(tools)) Reflect.apply(server.registerTool, server, [
23
+ tool.id,
24
+ {
25
+ title: tool.title,
26
+ description: tool.description,
27
+ inputSchema: tool.inputSchema,
28
+ annotations: tool.annotations,
29
+ _meta: { "neon/requiresApproval": tool.requiresApproval }
30
+ },
31
+ async (input, context) => {
32
+ try {
33
+ const apiKey = bearerCredentialFromMcpContext(context);
34
+ const structuredContent = { data: (await tool.execute(input, {
35
+ signal: signalFrom(context),
36
+ ...apiKey === void 0 ? {} : { apiKey }
37
+ })).data };
38
+ return {
39
+ content: [{
40
+ type: "text",
41
+ text: JSON.stringify(structuredContent)
42
+ }],
43
+ structuredContent
44
+ };
45
+ } catch (error) {
46
+ const structuredContent = { error: { message: errorMessage(error) } };
47
+ return {
48
+ content: [{
49
+ type: "text",
50
+ text: JSON.stringify(structuredContent)
51
+ }],
52
+ structuredContent,
53
+ isError: true
54
+ };
55
+ }
56
+ }
57
+ ]);
58
+ };
59
+ //#endregion
60
+ export { registerNeonTools };