a2a: discover the endpoint from an agent card
SPLINTER_A2A_CARD names the card; the first send picks the JSONRPC entry from supportedInterfaces, falls back to the older single url field, and keeps the discovered URL for the session. SPLINTER_A2A_URL still wins when both are set. The card fetch uses the same secret handling as a send: the bearer token goes through curl's stdin. The fake agent serves both card shapes, and test_a2a.sh covers discovery with and without supportedInterfaces.
This commit is contained in:
1 parent
93e955a4c7
commit
d95ac721be
10 files changed
+211
-20
No files matched your search
+152
-2
@@ -37,6 +37,7 @@
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <strings.h>
|
||||
#include "splinter.h"
|
||||
#include "splinter_plugin.h"
|
||||
#include "curl.h"
|
||||
@@ -44,6 +45,7 @@
|
||||
|
||||
struct a2a_cfg {
|
||||
char url[512];
|
||||
char card[512];
|
||||
char token[512];
|
||||
char task_id[128];
|
||||
char context_id[128];
|
||||
@@ -368,6 +370,145 @@ start_post(struct a2a_cfg *c, const char *body, int stream, pid_t *pid,
|
||||
return fd;
|
||||
}
|
||||
|
||||
/*
|
||||
* GET a document (the agent card) with the same secret handling as a send:
|
||||
* the bearer token travels on curl's stdin, never in argv.
|
||||
*/
|
||||
static int
|
||||
fetch(struct a2a_cfg *c, const char *url, struct sp_buf *body, long *http,
|
||||
struct sp_buf *err)
|
||||
{
|
||||
char tmo[32], auth[sizeof(c->token) + 32];
|
||||
const char *args[10];
|
||||
const char *hdrs[2] = { NULL, NULL };
|
||||
pid_t pid = -1;
|
||||
FILE *f;
|
||||
char chunk[4096];
|
||||
size_t got;
|
||||
int fd, status = 0;
|
||||
char *nl;
|
||||
|
||||
snprintf(tmo, sizeof(tmo), "%ld", c->timeout);
|
||||
args[0] = "-sS";
|
||||
args[1] = "-m";
|
||||
args[2] = tmo;
|
||||
args[3] = "-H";
|
||||
args[4] = "Accept: application/json";
|
||||
args[5] = "-w";
|
||||
args[6] = "\n%{http_code}";
|
||||
args[7] = NULL;
|
||||
if (c->token[0] != '\0') {
|
||||
snprintf(auth, sizeof(auth), "Authorization: Bearer %s",
|
||||
c->token);
|
||||
hdrs[0] = auth;
|
||||
}
|
||||
fd = sp_curl_start(args, url, hdrs, 0, &pid, err);
|
||||
memset(auth, 0, sizeof(auth));
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
f = fdopen(fd, "r");
|
||||
if (f == NULL) {
|
||||
close(fd);
|
||||
(void)kill(pid, SIGKILL);
|
||||
(void)waitpid(pid, NULL, 0);
|
||||
sp_buf_printf(err, "a2a: fdopen: %s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
while ((got = fread(chunk, 1, sizeof(chunk), f)) > 0)
|
||||
if (sp_buf_add(body, chunk, got) != 0) {
|
||||
fclose(f);
|
||||
(void)kill(pid, SIGKILL);
|
||||
(void)waitpid(pid, NULL, 0);
|
||||
sp_buf_puts(err, "a2a: out of memory");
|
||||
return -1;
|
||||
}
|
||||
fclose(f);
|
||||
if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) ||
|
||||
WEXITSTATUS(status) != 0) {
|
||||
sp_buf_printf(err, "a2a: card transfer failed for %s", url);
|
||||
return -1;
|
||||
}
|
||||
*http = 0;
|
||||
if (body->len < 2 || (nl = strrchr(body->p, '\n')) == NULL) {
|
||||
sp_buf_puts(err, "a2a: card response has no HTTP status");
|
||||
return -1;
|
||||
}
|
||||
*http = strtol(nl + 1, NULL, 10);
|
||||
*nl = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Discovery: the card names the interfaces. Prefer a JSONRPC one, else fall
|
||||
* back to the older single "url" field. SPLINTER_A2A_URL wins over the card.
|
||||
*/
|
||||
static int
|
||||
discover(struct a2a_cfg *c, struct sp_buf *err)
|
||||
{
|
||||
struct sp_buf body;
|
||||
mcp_json *v = NULL;
|
||||
const mcp_json *ifaces;
|
||||
const char *found = NULL;
|
||||
char url[512];
|
||||
long http = 0;
|
||||
size_t i, n;
|
||||
int rc = -1;
|
||||
|
||||
if (c->url[0] != '\0' || c->card[0] == '\0')
|
||||
return 0;
|
||||
memset(&body, 0, sizeof(body));
|
||||
if (sp_buf_init(&body) != 0)
|
||||
return -1;
|
||||
if (fetch(c, c->card, &body, &http, err) != 0)
|
||||
goto out;
|
||||
if (http < 200 || http > 299) {
|
||||
sp_buf_printf(err, "a2a: agent card HTTP %ld", http);
|
||||
goto out;
|
||||
}
|
||||
v = mcp_parse(body.p);
|
||||
if (v == NULL) {
|
||||
sp_buf_puts(err, "a2a: agent card is not JSON");
|
||||
goto out;
|
||||
}
|
||||
ifaces = mcp_arg(v, "supportedInterfaces");
|
||||
if (ifaces != NULL && ifaces->t == MCP_ARR) {
|
||||
n = mcp_array_len(ifaces);
|
||||
for (i = 0; i < n; i++) {
|
||||
const mcp_json *it = mcp_array_at(ifaces, i);
|
||||
const char *b = mcp_arg_string(it, "protocolBinding");
|
||||
const char *u = mcp_arg_string(it, "url");
|
||||
|
||||
if (u == NULL || *u == '\0')
|
||||
continue;
|
||||
if (b == NULL || strcasecmp(b, "JSONRPC") == 0) {
|
||||
snprintf(url, sizeof(url), "%s", u);
|
||||
found = url;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (found == NULL) {
|
||||
const char *u = mcp_arg_string(v, "url");
|
||||
|
||||
if (u != NULL && *u != '\0') {
|
||||
snprintf(url, sizeof(url), "%s", u);
|
||||
found = url;
|
||||
}
|
||||
}
|
||||
if (found == NULL) {
|
||||
sp_buf_puts(err, "a2a: agent card names no JSON-RPC interface");
|
||||
goto out;
|
||||
}
|
||||
snprintf(c->url, sizeof(c->url), "%s", found);
|
||||
rc = 0;
|
||||
|
||||
out:
|
||||
if (v != NULL)
|
||||
mcp_json_free(v);
|
||||
sp_buf_free(&body);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
post(struct a2a_cfg *c, const char *body, struct sp_buf *raw,
|
||||
struct sp_buf *err)
|
||||
@@ -637,8 +778,11 @@ a2a_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
||||
|
||||
if (c == NULL || conv == NULL || out == NULL)
|
||||
return -1;
|
||||
if (discover(c, err) != 0)
|
||||
return -1;
|
||||
if (c->url[0] == '\0') {
|
||||
sp_buf_puts(err, "a2a: SPLINTER_A2A_URL is not set");
|
||||
sp_buf_puts(err, "a2a: neither SPLINTER_A2A_URL nor "
|
||||
"SPLINTER_A2A_CARD is set");
|
||||
return -1;
|
||||
}
|
||||
memset(out, 0, sizeof(*out));
|
||||
@@ -759,8 +903,11 @@ a2a_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
||||
|
||||
if (c == NULL || conv == NULL || out == NULL)
|
||||
return -1;
|
||||
if (discover(c, err) != 0)
|
||||
return -1;
|
||||
if (c->url[0] == '\0') {
|
||||
sp_buf_puts(err, "a2a: SPLINTER_A2A_URL is not set");
|
||||
sp_buf_puts(err, "a2a: neither SPLINTER_A2A_URL nor "
|
||||
"SPLINTER_A2A_CARD is set");
|
||||
return -1;
|
||||
}
|
||||
memset(out, 0, sizeof(*out));
|
||||
@@ -819,6 +966,7 @@ a2a_init(struct sp_host *h)
|
||||
static struct a2a_cfg cfg;
|
||||
static struct sp_transport tr;
|
||||
const char *url = getenv("SPLINTER_A2A_URL");
|
||||
const char *card = getenv("SPLINTER_A2A_CARD");
|
||||
const char *tok = getenv("SPLINTER_A2A_TOKEN");
|
||||
const char *to = getenv("SPLINTER_A2A_TIMEOUT");
|
||||
const char *full = getenv("SPLINTER_A2A_FULL");
|
||||
@@ -826,6 +974,8 @@ a2a_init(struct sp_host *h)
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
if (url != NULL)
|
||||
snprintf(cfg.url, sizeof(cfg.url), "%s", url);
|
||||
if (card != NULL)
|
||||
snprintf(cfg.card, sizeof(cfg.card), "%s", card);
|
||||
if (tok != NULL)
|
||||
snprintf(cfg.token, sizeof(cfg.token), "%s", tok);
|
||||
cfg.timeout = 300;
|
||||
|
||||
Reference in new issue
Block a user