Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f0b2d9ead8 |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "a2amcp",
|
"name": "a2amcp",
|
||||||
"version": "0.1.8",
|
"version": "0.1.9",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "a2amcp",
|
"name": "a2amcp",
|
||||||
"version": "0.1.8",
|
"version": "0.1.9",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@modelcontextprotocol/sdk": "^1.0.4",
|
"@modelcontextprotocol/sdk": "^1.0.4",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "a2amcp",
|
"name": "a2amcp",
|
||||||
"version": "0.1.8",
|
"version": "0.1.9",
|
||||||
"description": "MCP gateway for a2acloud agents. Run as a local MCP server; expose any number of deployed A2A agents to Claude Code, Cursor, and other MCP clients.",
|
"description": "MCP gateway for a2acloud agents. Run as a local MCP server; expose any number of deployed A2A agents to Claude Code, Cursor, and other MCP clients.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"bin": {
|
"bin": {
|
||||||
|
|||||||
32
src/api.ts
32
src/api.ts
@@ -24,8 +24,11 @@ export class ControlPlaneClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private headers(): Record<string, string> {
|
private headers(): Record<string, string> {
|
||||||
const h: Record<string, string> = { "content-type": "application/json" };
|
const h: Record<string, string> = {
|
||||||
if (this.token) h["authorization"] = `bearer ${this.token}`;
|
accept: "application/json",
|
||||||
|
"content-type": "application/json",
|
||||||
|
};
|
||||||
|
if (this.token) h["authorization"] = `Bearer ${this.token}`;
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,18 +38,12 @@ export class ControlPlaneClient {
|
|||||||
headers: this.headers(),
|
headers: this.headers(),
|
||||||
body: body !== undefined ? JSON.stringify(body) : undefined,
|
body: body !== undefined ? JSON.stringify(body) : undefined,
|
||||||
});
|
});
|
||||||
|
const text = await resp.text();
|
||||||
if (!resp.ok) {
|
if (!resp.ok) {
|
||||||
let detail: string;
|
throw new ApiError(resp.status, errorDetail(text, resp.statusText));
|
||||||
try {
|
|
||||||
const j: any = await resp.json();
|
|
||||||
detail = j?.detail ?? JSON.stringify(j);
|
|
||||||
} catch {
|
|
||||||
detail = await resp.text();
|
|
||||||
}
|
|
||||||
throw new ApiError(resp.status, detail);
|
|
||||||
}
|
}
|
||||||
if (resp.status === 204) return undefined as T;
|
if (resp.status === 204) return undefined as T;
|
||||||
return (await resp.json()) as T;
|
return (text ? JSON.parse(text) : undefined) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
me() {
|
me() {
|
||||||
@@ -61,3 +58,16 @@ export class ControlPlaneClient {
|
|||||||
return this.request<AgentRow>("GET", `/v1/agents/${encodeURIComponent(name)}`);
|
return this.request<AgentRow>("GET", `/v1/agents/${encodeURIComponent(name)}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function errorDetail(text: string, fallback: string): string {
|
||||||
|
if (!text.trim()) return fallback;
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(text);
|
||||||
|
const detail = parsed?.detail;
|
||||||
|
if (typeof detail === "string") return detail;
|
||||||
|
if (detail !== undefined) return JSON.stringify(detail);
|
||||||
|
return JSON.stringify(parsed);
|
||||||
|
} catch {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
15
src/oauth.ts
15
src/oauth.ts
@@ -208,9 +208,10 @@ async function exchangeAuthorizationCode(opts: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function parseTokenResponse(resp: Response): Promise<TokenResponse> {
|
async function parseTokenResponse(resp: Response): Promise<TokenResponse> {
|
||||||
const data = (await resp.json().catch(() => ({}))) as TokenResponse;
|
const text = await resp.text();
|
||||||
|
const data = parseJsonObject(text) as TokenResponse;
|
||||||
if (!resp.ok || !data.access_token) {
|
if (!resp.ok || !data.access_token) {
|
||||||
const message = data.error_description || data.error || resp.statusText;
|
const message = data.error_description || data.error || text.trim() || resp.statusText;
|
||||||
throw new Error(`Keycloak token exchange failed: ${message}`);
|
throw new Error(`Keycloak token exchange failed: ${message}`);
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
@@ -336,3 +337,13 @@ function expiresAt(expiresIn: number | undefined): number | undefined {
|
|||||||
function nowSeconds(): number {
|
function nowSeconds(): number {
|
||||||
return Math.floor(Date.now() / 1000);
|
return Math.floor(Date.now() / 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseJsonObject(text: string): Record<string, unknown> {
|
||||||
|
if (!text.trim()) return {};
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(text);
|
||||||
|
return parsed && typeof parsed === "object" ? parsed : {};
|
||||||
|
} catch {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
50
tests/api.test.ts
Normal file
50
tests/api.test.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import { test, beforeEach, afterEach } from "node:test";
|
||||||
|
import assert from "node:assert/strict";
|
||||||
|
|
||||||
|
import { ApiError, ControlPlaneClient } from "../src/api.js";
|
||||||
|
|
||||||
|
const originalFetch = globalThis.fetch;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
globalThis.fetch = originalFetch;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
globalThis.fetch = originalFetch;
|
||||||
|
});
|
||||||
|
|
||||||
|
test("ControlPlaneClient reports non-JSON error bodies without rereading them", async () => {
|
||||||
|
globalThis.fetch = (async () =>
|
||||||
|
new Response("<h1>upstream unavailable</h1>", {
|
||||||
|
status: 502,
|
||||||
|
statusText: "Bad Gateway",
|
||||||
|
headers: { "content-type": "text/html" },
|
||||||
|
})) as typeof fetch;
|
||||||
|
|
||||||
|
await assert.rejects(
|
||||||
|
() => new ControlPlaneClient("https://api.example.test", "token").me(),
|
||||||
|
(err) => {
|
||||||
|
assert.ok(err instanceof ApiError);
|
||||||
|
assert.equal(err.status, 502);
|
||||||
|
assert.match(err.message, /upstream unavailable/);
|
||||||
|
assert.doesNotMatch(err.message, /Body is unusable/);
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("ControlPlaneClient forwards bearer tokens and extracts JSON error detail", async () => {
|
||||||
|
globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
|
||||||
|
const headers = init?.headers as Record<string, string>;
|
||||||
|
assert.equal(headers.authorization, "Bearer access-token");
|
||||||
|
return new Response(JSON.stringify({ detail: "keycloak email is not verified" }), {
|
||||||
|
status: 403,
|
||||||
|
headers: { "content-type": "application/json" },
|
||||||
|
});
|
||||||
|
}) as typeof fetch;
|
||||||
|
|
||||||
|
await assert.rejects(
|
||||||
|
() => new ControlPlaneClient("https://api.example.test", "access-token").me(),
|
||||||
|
/API 403: keycloak email is not verified/,
|
||||||
|
);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user