@promptlayer/mcp-server 1.0.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/src/utils.ts ADDED
@@ -0,0 +1,65 @@
1
+ import { PromptLayerClient } from "./client.js";
2
+
3
+ export function buildQueryParams(params?: Record<string, unknown>): string {
4
+ if (!params) return "";
5
+ const sp = new URLSearchParams();
6
+ for (const [key, value] of Object.entries(params)) {
7
+ if (value !== undefined && value !== null) sp.set(key, String(value));
8
+ }
9
+ const q = sp.toString();
10
+ return q ? `?${q}` : "";
11
+ }
12
+
13
+ export function getApiKey(providedKey?: string): string {
14
+ const apiKey = providedKey ?? process.env.PROMPTLAYER_API_KEY;
15
+ if (!apiKey) {
16
+ throw new Error("No API key provided. Set PROMPTLAYER_API_KEY environment variable or pass api_key parameter.");
17
+ }
18
+ if (!apiKey.startsWith("pl_")) {
19
+ throw new Error("Invalid API key format. PromptLayer API keys must start with 'pl_'");
20
+ }
21
+ return apiKey;
22
+ }
23
+
24
+ export async function handleApiError(response: Response): Promise<Error> {
25
+ let msg: string;
26
+ try {
27
+ const body = await response.text();
28
+ const parsed = JSON.parse(body);
29
+ msg = parsed.message || parsed.error || parsed.detail || body;
30
+ } catch {
31
+ msg = response.statusText || "Unknown error";
32
+ }
33
+ const status = response.status;
34
+ if (status === 401) return new Error("Invalid PromptLayer API key");
35
+ if (status === 403) return new Error("Permission denied. Check your API key permissions.");
36
+ if (status === 404) return new Error(`Resource not found: ${msg}`);
37
+ if (status === 422) return new Error(`Validation error: ${msg}`);
38
+ if (status === 429) return new Error("Rate limit exceeded. Please try again later.");
39
+ if (status >= 500) return new Error(`PromptLayer service error: ${msg}`);
40
+ return new Error(`PromptLayer API error (${status}): ${msg}`);
41
+ }
42
+
43
+ type Args = Record<string, unknown> & { api_key?: string };
44
+
45
+ export function createToolHandler(
46
+ call: (client: PromptLayerClient, args: Args) => Promise<unknown>,
47
+ msg: (result: unknown) => string
48
+ ) {
49
+ return async (args: Args) => {
50
+ try {
51
+ const client = new PromptLayerClient(getApiKey(args.api_key));
52
+ const result = await call(client, args);
53
+ const json = JSON.stringify(result, null, 2);
54
+ return {
55
+ content: [{ type: "text" as const, text: `${msg(result)}\n\n${json}` }],
56
+ structuredContent: result,
57
+ };
58
+ } catch (error) {
59
+ return {
60
+ content: [{ type: "text" as const, text: `Error: ${error instanceof Error ? error.message : error}` }],
61
+ isError: true,
62
+ };
63
+ }
64
+ };
65
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "ESNext",
5
+ "lib": ["ES2022"],
6
+ "moduleResolution": "node",
7
+ "outDir": "./build",
8
+ "rootDir": "./src",
9
+ "strict": true,
10
+ "esModuleInterop": true,
11
+ "skipLibCheck": true,
12
+ "forceConsistentCasingInFileNames": true,
13
+ "resolveJsonModule": true,
14
+ "declaration": true,
15
+ "declarationMap": true,
16
+ "sourceMap": true,
17
+ "noUnusedLocals": true,
18
+ "noUnusedParameters": true,
19
+ "noImplicitReturns": true,
20
+ "noFallthroughCasesInSwitch": true
21
+ },
22
+ "include": ["src/**/*"],
23
+ "exclude": ["node_modules", "build"]
24
+ }