The MCP client could only start one program and talk to it on a pipe; a server behind an endpoint (junction at http://host:8090/mcp) had no way in. Now the config names the servers and the plugin speaks whichever transport each one offers: - new `mcp NAME { url | cmd | header | enabled }` block (and the flat `mcp = NAME | url=...` form), parsed into struct sp_mcpserver; an entry without a url and without a cmd is refused with its line number - streamable HTTP in plugins/mcp/mcp.c: one POST per JSON-RPC message, answered with one JSON object or an SSE stream of `data:` lines - both are read, Mcp-Session-Id is kept and echoed back, and the Authorization header travels through curl's stdin config so the token never appears in ps - several servers register side by side; tools are mcp_<tool> for one server and mcp_<server>_<tool> for more, so two servers that both offer `search` do not overwrite each other. A server that fails its handshake is reported and skipped, the rest still load. SPLINTER_MCP_CMD stays as the one stdio server - tests: the config and block parsers, and a socket server in test_mcpclient that refuses the tool list without the configured header and answers the call as an event stream - README and docs/CONFIG.md describe both transports and where the token lives
144 lines
4.3 KiB
C
144 lines
4.3 KiB
C
/*
|
|
* splinter -- the block syntax of the config file.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
#include "config.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
static int
|
|
load(const char *text, struct sp_config *c, struct sp_buf *err)
|
|
{
|
|
char path[] = "/var/tmp/splinter-cb-XXXXXX";
|
|
int fd = mkstemp(path), rc;
|
|
ssize_t w;
|
|
|
|
if (fd < 0)
|
|
return -2;
|
|
w = write(fd, text, strlen(text));
|
|
(void)w;
|
|
close(fd);
|
|
sp_config_init(c);
|
|
rc = sp_config_load(c, path, err);
|
|
unlink(path);
|
|
return rc;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
struct sp_config *c = calloc(1, sizeof(*c));
|
|
struct sp_buf err;
|
|
|
|
sp_buf_init(&err);
|
|
setenv("CB_TOKEN", "sekrit", 1);
|
|
unsetenv("CB_UNSET");
|
|
|
|
CHECK(load(
|
|
"# a comment\n"
|
|
"core {\n"
|
|
" theme = Delphinium\n"
|
|
" steps = 40\n"
|
|
" autonomous = true\n"
|
|
" default = claude\n"
|
|
" plugins = [a.so, \"b c.so\"]\n"
|
|
" on_limit = wait\n"
|
|
" max_wait = 6h\n"
|
|
"}\n"
|
|
"telegram { allow = [1, 2] }\n"
|
|
"provider local {\n"
|
|
" about = \"the local one\"\n"
|
|
" endpoint = http://h:1/v1/chat/completions\n"
|
|
" model = m\n"
|
|
" tags = [local, code]\n"
|
|
" prio = 5\n"
|
|
" limit = 10/5h\n"
|
|
"}\n"
|
|
"provider claude-a {\n"
|
|
" kind = cli\n"
|
|
" cmd = \"claude -p\"\n"
|
|
" tags = [cli, big]\n"
|
|
" env { CLAUDE_CODE_OAUTH_TOKEN = \"${CB_TOKEN}\" OTHER = \"${CB_UNSET}\" }\n"
|
|
"}\n"
|
|
"provider hidden { endpoint = http://x/v1 model = z delegate = false }\n"
|
|
"pool claude { members = [claude-a, local] strategy = balance }\n"
|
|
"mcp junction {\n"
|
|
" url = http://127.0.0.1:8090/mcp\n"
|
|
" header = \"Authorization: Bearer ${CB_TOKEN}\"\n"
|
|
"}\n"
|
|
"mcp files {\n"
|
|
" cmd = \"npx -y server-filesystem /home\"\n"
|
|
" enabled = false\n"
|
|
"}\n",
|
|
c, &err) == 0);
|
|
if (err.len > 0) fprintf(stderr, "err: %s\n", err.p);
|
|
CHECK(strcmp(c->theme, "Delphinium") == 0);
|
|
CHECK(c->steps == 40);
|
|
CHECK(c->autonomous == 1);
|
|
CHECK(strcmp(c->default_id, "claude") == 0);
|
|
CHECK(c->nplugins == 2 && strcmp(c->plugins[1], "b c.so") == 0);
|
|
CHECK(c->max_wait == 6 * 3600);
|
|
CHECK(strcmp(c->telegram_allow, "1,2") == 0);
|
|
CHECK(c->ndelegates == 2);
|
|
CHECK(strcmp(c->delegate[0].id, "local") == 0);
|
|
CHECK(strcmp(c->delegate[0].about, "the local one") == 0);
|
|
CHECK(strcmp(c->delegate[0].tags, "local,code") == 0);
|
|
CHECK(c->delegate[0].req_limit == 10 && c->delegate[0].req_window == 18000);
|
|
CHECK(c->delegate[1].nenv == 1);
|
|
CHECK(strstr(c->delegate[1].env[0], "=sekrit") != NULL);
|
|
CHECK(strcmp(c->delegate[1].kind, "cli") == 0);
|
|
CHECK(c->npools == 1 && c->pool[0].nmembers == 2);
|
|
CHECK(strcmp(c->pool[0].strategy, "balance") == 0);
|
|
CHECK(c->nmcp == 2);
|
|
CHECK(strcmp(c->mcp[0].id, "junction") == 0);
|
|
CHECK(strcmp(c->mcp[0].url, "http://127.0.0.1:8090/mcp") == 0);
|
|
CHECK(strstr(c->mcp[0].header, "Bearer sekrit") != NULL);
|
|
CHECK(c->mcp[0].enabled == 1);
|
|
CHECK(strcmp(c->mcp[1].id, "files") == 0);
|
|
CHECK(strcmp(c->mcp[1].cmd, "npx -y server-filesystem /home") == 0);
|
|
CHECK(c->mcp[1].enabled == 0);
|
|
CHECK(c->mcp[1].url[0] == '\0');
|
|
|
|
sp_buf_clear(&err);
|
|
CHECK(load("mcp noendpoint { header = \"x: y\" }\n", c, &err) != 0);
|
|
CHECK(strstr(err.p, "url or a cmd") != NULL);
|
|
sp_buf_clear(&err);
|
|
CHECK(load("mcp { url = http://h/mcp }\n", c, &err) != 0);
|
|
|
|
/* errors name the line that was written */
|
|
sp_buf_clear(&err);
|
|
CHECK(load("core {\n steps = 1\n bogus_key = 2\n}\n", c, &err) != 0);
|
|
CHECK(strstr(err.p, ":3:") != NULL && strstr(err.p, "bogus_key") != NULL);
|
|
sp_buf_clear(&err);
|
|
CHECK(load("provider x {\n nope = 1\n}\n", c, &err) != 0);
|
|
CHECK(strstr(err.p, ":2:") != NULL && strstr(err.p, "nope") != NULL);
|
|
sp_buf_clear(&err);
|
|
CHECK(load("core {\n steps = 1\n", c, &err) != 0);
|
|
CHECK(strstr(err.p, "closing }") != NULL);
|
|
sp_buf_clear(&err);
|
|
CHECK(load("pool p { strategy = balance }\n", c, &err) != 0);
|
|
sp_buf_clear(&err);
|
|
CHECK(load("provider p { about = \"a|b\" }\n", c, &err) != 0);
|
|
|
|
/* the flat form still loads */
|
|
sp_buf_clear(&err);
|
|
CHECK(load("model = m\nsteps = 7\n", c, &err) == 0 && c->steps == 7);
|
|
|
|
free(c);
|
|
if (failures == 0)
|
|
puts("test_confblock: ok");
|
|
return failures != 0;
|
|
}
|