http: Transport aus libmcp benutzen statt eigener Kopie

Der HTTP-Teil (211 Zeilen) ist in libmcp gewandert; tasksd ruft jetzt
mcp_serve_http und mcp_parse_port. Kein Verhalten geändert, nur der Ort.
This commit is contained in:
Hermes committed 2026-11-06 19:03:15 +00:00
1 parent 91cb1dcdde
commit 82b6acf344
2 files changed
+241 -219

No files matched your search

+2 -216
View File
@@ -812,217 +812,6 @@ static const mcp_tool TOOLS[] = {
t_drop },
};
/* ------------------------------------------------------------------ http */
/* strcasestr is a GNU extension, not POSIX: do it here. */
static char *ci_strstr(const char *hay, const char *needle)
{
size_t nl = strlen(needle);
const char *p;
if (nl == 0)
return (char *)hay;
for (p = hay; *p; p++) {
size_t i;
for (i = 0; i < nl; i++) {
if (p[i] == '\0' ||
tolower((unsigned char)p[i]) != tolower((unsigned char)needle[i]))
break;
}
if (i == nl)
return (char *)p;
}
return NULL;
}
static void http_reply(int fd, int code, const char *ctype, const char *body,
const char *session)
{
mcp_buf out;
size_t len = body != NULL ? strlen(body) : 0;
size_t sent = 0;
mcp_buf_init(&out);
mcp_buf_printf(&out, "HTTP/1.1 %d %s\r\n", code,
code == 200 ? "OK" : (code == 202 ? "Accepted" : "Method Not Allowed"));
if (ctype != NULL)
mcp_buf_printf(&out, "Content-Type: %s\r\n", ctype);
if (session != NULL)
mcp_buf_printf(&out, "Mcp-Session-Id: %s\r\n", session);
mcp_buf_printf(&out, "Content-Length: %lu\r\n", (unsigned long)len);
mcp_buf_puts(&out, "Connection: close\r\n\r\n");
if (len > 0)
mcp_buf_add(&out, body, len);
while (sent < out.len) {
ssize_t w = write(fd, out.p + sent, out.len - sent);
if (w <= 0)
break;
sent += (size_t)w;
}
mcp_buf_free(&out);
}
static void serve_one(int fd, mcp_server *srv, const char *session)
{
char buf[8192];
mcp_buf head;
int header_done = 0;
long content_length = 0;
char *body = NULL;
char *reply;
mcp_buf_init(&head);
for (;;) {
ssize_t r = read(fd, buf, sizeof(buf));
if (r <= 0)
break;
mcp_buf_add(&head, buf, (size_t)r);
{
char *sep = strstr(head.p, "\r\n\r\n");
if (sep != NULL) {
char *cl = ci_strstr(head.p, "Content-Length:");
size_t header_len = (size_t)(sep + 4 - head.p);
size_t have = head.len > header_len ? head.len - header_len : 0;
header_done = 1;
if (cl != NULL)
content_length = strtol(cl + 15, NULL, 10);
if (content_length < 0 || content_length > MAX_BODY) {
http_reply(fd, 405, "text/plain", "body too large\n", NULL);
mcp_buf_free(&head);
close(fd);
return;
}
body = calloc((size_t)content_length + 1, 1);
if (body == NULL) {
mcp_buf_free(&head);
close(fd);
return;
}
if (have > (size_t)content_length)
have = (size_t)content_length;
memcpy(body, head.p + header_len, have);
while (have < (size_t)content_length) {
ssize_t r2 = read(fd, body + have, (size_t)content_length - have);
if (r2 <= 0)
break;
have += (size_t)r2;
}
body[have] = '\0';
break;
}
}
if (head.len > MAX_BODY)
break;
}
if (!header_done) {
http_reply(fd, 405, "text/plain", "want POST /mcp\n", NULL);
mcp_buf_free(&head);
close(fd);
return;
}
if (strncmp(head.p, "GET ", 4) == 0 || strncmp(head.p, "DELETE ", 7) == 0) {
/* the SSE stream is not offered: this server answers per request */
http_reply(fd, 202, NULL, "", session);
free(body);
mcp_buf_free(&head);
close(fd);
return;
}
if (strncmp(head.p, "POST ", 5) != 0) {
http_reply(fd, 405, "text/plain", "only POST\n", NULL);
free(body);
mcp_buf_free(&head);
close(fd);
return;
}
reply = mcp_handle(srv, body != NULL ? body : "{}");
free(body);
if (reply == NULL) {
http_reply(fd, 202, NULL, "", session); /* notification: nothing to say */
} else {
http_reply(fd, 200, "application/json", reply, session);
free(reply);
}
mcp_buf_free(&head);
close(fd);
}
static int serve_http(mcp_server *srv, int port)
{
int ls;
struct sockaddr_in addr;
int one = 1;
char session[40];
snprintf(session, sizeof(session), "%08x-%08x",
(unsigned)time(NULL), (unsigned)getpid());
ls = socket(AF_INET, SOCK_STREAM, 0);
if (ls < 0) {
perror("socket");
return 1;
}
setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons((unsigned short)port);
if (bind(ls, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
perror("bind");
close(ls);
return 1;
}
if (listen(ls, 64) != 0) {
perror("listen");
close(ls);
return 1;
}
/* fork per connection: without this every finished child stays a zombie */
signal(SIGCHLD, SIG_IGN);
fprintf(stderr, "tasksd: http mcp on 0.0.0.0:%d (session %s)\n", port, session);
for (;;) {
int fd = accept(ls, NULL, NULL);
pid_t pid;
if (fd < 0)
continue;
pid = fork();
if (pid == 0) {
close(ls);
serve_one(fd, srv, session);
_exit(0);
}
if (pid > 0)
close(fd);
else
serve_one(fd, srv, session);
}
}
/* "--http 8781", "--http :8781" und "--http 0.0.0.0:8781" sind alle gemeint.
* atoi(":8781") ist 0 — mit dem Doppelpunkt vor der Zahl band tasksd auf einem
* freien Zufallsport, und niemand kam mehr heran. Also erst hinter den letzten
* Doppelpunkt springen, dann die Zahl lesen. */
static int parse_port(const char *s)
{
const char *p = strrchr(s, ':');
int port = atoi(p ? p + 1 : s);
if (port <= 0 || port > 65535) {
fprintf(stderr, "tasksd: unbrauchbarer Port in '%s' "
"(erwartet 8781, :8781 oder 0.0.0.0:8781)\n", s);
return -1;
}
return port;
}
int main(int argc, char **argv)
{
mcp_server srv;
@@ -1034,14 +823,11 @@ int main(int argc, char **argv)
srv.user = NULL;
if (argc >= 3 && strcmp(argv[1], "--http") == 0) {
int port = parse_port(argv[2]);
int port = mcp_parse_port(argv[2]);
if (port < 0)
return 2;
if (port < 1024)
fprintf(stderr, "tasksd: Warnung: Port %d liegt im "
"privilegierten Bereich\n", port);
return serve_http(&srv, port);
return mcp_serve_http(&srv, NULL, port);
}
return mcp_serve_stdio(&srv);
}
+239 -3
View File
@@ -1,3 +1,6 @@
#ifndef MCP_H
#define MCP_H
/*
* libmcp - a minimal Model Context Protocol server in C99 + POSIX.
*
@@ -34,8 +37,6 @@
* A tool that does not reply gets an error reply from the library, so a
* forgotten branch cannot make a client hang.
*/
#ifndef MCP_H
#define MCP_H
#include <stddef.h>
@@ -83,6 +84,18 @@ typedef struct {
/* Serve on stdin/stdout until EOF. Returns 0. */
int mcp_serve_stdio(mcp_server *srv);
/* Serve the same tools over HTTP (Streamable HTTP, JSON-RPC 2.0 on POST).
* host may be NULL for "0.0.0.0". Returns 0 if the socket could not be opened;
* otherwise it never returns — it accepts connections until the process ends.
*
* One process per connection via fork(): without that, every finished child
* stays a zombie and a long-running server grows a process table full of them. */
int mcp_serve_http(mcp_server *srv, const char *host, int port);
/* "--http 8781", "--http :8781" and "--http 0.0.0.0:8781" all mean the same.
* Returns -1 (and prints why) for anything that is not a usable port. */
int mcp_parse_port(const char *s);
/* ---------------------------------------------------------------- replies */
/* Reply with a single text content block (the usual tool answer). */
@@ -142,18 +155,29 @@ char *mcp_handle(mcp_server *srv, const char *line);
}
#endif
#endif /* MCP_H */
/* ===========================================================================
* implementation
* =========================================================================== */
#ifdef MCP_IMPLEMENTATION
#ifndef MCP_MAX_BODY
#define MCP_MAX_BODY (8u * 1024u * 1024u)
#endif
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <netinet/in.h>
#include <signal.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#ifndef MCP_MAX_TOOLS
#define MCP_MAX_TOOLS 128
@@ -895,4 +919,216 @@ int mcp_serve_stdio(mcp_server *srv)
return 0;
}
/* strcasestr is a GNU extension, not POSIX: do it here. */
static char *mcp_ci_strstr(const char *hay, const char *needle)
{
size_t nl = strlen(needle);
const char *p;
if (nl == 0)
return (char *)hay;
for (p = hay; *p; p++) {
size_t i;
for (i = 0; i < nl; i++) {
if (p[i] == '\0' ||
tolower((unsigned char)p[i]) != tolower((unsigned char)needle[i]))
break;
}
if (i == nl)
return (char *)p;
}
return NULL;
}
static void mcp_http_reply(int fd, int code, const char *ctype, const char *body,
const char *session)
{
mcp_buf out;
size_t len = body != NULL ? strlen(body) : 0;
size_t sent = 0;
mcp_buf_init(&out);
mcp_buf_printf(&out, "HTTP/1.1 %d %s\r\n", code,
code == 200 ? "OK" : (code == 202 ? "Accepted" : "Method Not Allowed"));
if (ctype != NULL)
mcp_buf_printf(&out, "Content-Type: %s\r\n", ctype);
if (session != NULL)
mcp_buf_printf(&out, "Mcp-Session-Id: %s\r\n", session);
mcp_buf_printf(&out, "Content-Length: %lu\r\n", (unsigned long)len);
mcp_buf_puts(&out, "Connection: close\r\n\r\n");
if (len > 0)
mcp_buf_add(&out, body, len);
while (sent < out.len) {
ssize_t w = write(fd, out.p + sent, out.len - sent);
if (w <= 0)
break;
sent += (size_t)w;
}
mcp_buf_free(&out);
}
static void mcp_http_serve_one(int fd, mcp_server *srv, const char *session)
{
char buf[8192];
mcp_buf head;
int header_done = 0;
long content_length = 0;
char *body = NULL;
char *reply;
mcp_buf_init(&head);
for (;;) {
ssize_t r = read(fd, buf, sizeof(buf));
if (r <= 0)
break;
mcp_buf_add(&head, buf, (size_t)r);
{
char *sep = strstr(head.p, "\r\n\r\n");
if (sep != NULL) {
char *cl = mcp_ci_strstr(head.p, "Content-Length:");
size_t header_len = (size_t)(sep + 4 - head.p);
size_t have = head.len > header_len ? head.len - header_len : 0;
header_done = 1;
if (cl != NULL)
content_length = strtol(cl + 15, NULL, 10);
if (content_length < 0 || content_length > MCP_MAX_BODY) {
mcp_http_reply(fd, 405, "text/plain", "body too large\n", NULL);
mcp_buf_free(&head);
close(fd);
return;
}
body = calloc((size_t)content_length + 1, 1);
if (body == NULL) {
mcp_buf_free(&head);
close(fd);
return;
}
if (have > (size_t)content_length)
have = (size_t)content_length;
memcpy(body, head.p + header_len, have);
while (have < (size_t)content_length) {
ssize_t r2 = read(fd, body + have, (size_t)content_length - have);
if (r2 <= 0)
break;
have += (size_t)r2;
}
body[have] = '\0';
break;
}
}
if (head.len > MCP_MAX_BODY)
break;
}
if (!header_done) {
mcp_http_reply(fd, 405, "text/plain", "want POST /mcp\n", NULL);
mcp_buf_free(&head);
close(fd);
return;
}
if (strncmp(head.p, "GET ", 4) == 0 || strncmp(head.p, "DELETE ", 7) == 0) {
/* the SSE stream is not offered: this server answers per request */
mcp_http_reply(fd, 202, NULL, "", session);
free(body);
mcp_buf_free(&head);
close(fd);
return;
}
if (strncmp(head.p, "POST ", 5) != 0) {
mcp_http_reply(fd, 405, "text/plain", "only POST\n", NULL);
free(body);
mcp_buf_free(&head);
close(fd);
return;
}
reply = mcp_handle(srv, body != NULL ? body : "{}");
free(body);
if (reply == NULL) {
mcp_http_reply(fd, 202, NULL, "", session); /* notification: nothing to say */
} else {
mcp_http_reply(fd, 200, "application/json", reply, session);
free(reply);
}
mcp_buf_free(&head);
close(fd);
}
int mcp_serve_http(mcp_server *srv, const char *host, int port)
{
int ls;
struct sockaddr_in addr;
int one = 1;
char session[40];
snprintf(session, sizeof(session), "%08x-%08x",
(unsigned)time(NULL), (unsigned)getpid());
ls = socket(AF_INET, SOCK_STREAM, 0);
if (ls < 0) {
perror("socket");
return 1;
}
setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
if (inet_pton(AF_INET, host ? host : "0.0.0.0", &addr.sin_addr) != 1)
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons((unsigned short)port);
if (bind(ls, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
perror("bind");
close(ls);
return 1;
}
if (listen(ls, 64) != 0) {
perror("listen");
close(ls);
return 1;
}
/* fork per connection: without this every finished child stays a zombie */
signal(SIGCHLD, SIG_IGN);
fprintf(stderr, "%s: http mcp on %s:%d (session %s)\n",
srv->name ? srv->name : "mcp", host ? host : "0.0.0.0", port, session);
for (;;) {
int fd = accept(ls, NULL, NULL);
pid_t pid;
if (fd < 0)
continue;
pid = fork();
if (pid == 0) {
close(ls);
mcp_http_serve_one(fd, srv, session);
_exit(0);
}
if (pid > 0)
close(fd);
else
mcp_http_serve_one(fd, srv, session);
}
}
/* "--http 8781", "--http :8781" und "--http 0.0.0.0:8781" sind alle gemeint.
* atoi(":8781") is 0 — with the colon in front the server bound to a random
* free port that nobody could find. Skip past the last colon, then read. */
int mcp_parse_port(const char *s)
{
const char *p = strrchr(s, ':');
int port = atoi(p ? p + 1 : s);
if (port <= 0 || port > 65535) {
fprintf(stderr, "mcp: unusable port in '%s' (want 8781, :8781 or 0.0.0.0:8781)\n", s);
return -1;
}
return port;
}
#endif /* MCP_IMPLEMENTATION */
#endif /* MCP_H */