Files
fil/docs/snippets/c/mcp/mcp_custom_client.md
Henrik Jess Nielsen b4c07d3693
All checks were successful
Deploy fil (kreuzberg) / deploy (push) Successful in 49s
Nomad changes
2026-06-01 23:40:55 +02:00

1.5 KiB

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/* The kreuzberg C FFI does not bundle an MCP client. Drive the kreuzberg
 * CLI's stdio MCP transport from a C host that also links libkreuzberg. */
int main(void) {
    int request_pipe[2];
    int response_pipe[2];
    if (pipe(request_pipe) < 0 || pipe(response_pipe) < 0) {
        perror("pipe");
        return 1;
    }

    pid_t pid = fork();
    if (pid < 0) {
        perror("fork");
        return 1;
    }
    if (pid == 0) {
        dup2(request_pipe[0], 0);
        dup2(response_pipe[1], 1);
        close(request_pipe[1]);
        close(response_pipe[0]);
        execlp("kreuzberg", "kreuzberg", "mcp", (char *)NULL);
        perror("execlp");
        _exit(127);
    }

    close(request_pipe[0]);
    close(response_pipe[1]);

    const char *request =
        "{\"method\":\"tools/call\","
        "\"params\":{\"name\":\"extract_file\","
        "\"arguments\":{\"path\":\"document.pdf\",\"async\":true}}}\n";
    if (write(request_pipe[1], request, strlen(request)) < 0) {
        perror("write");
        return 1;
    }
    close(request_pipe[1]);

    char buffer[4096];
    ssize_t bytes_read = read(response_pipe[0], buffer, sizeof(buffer) - 1);
    if (bytes_read > 0) {
        buffer[bytes_read] = '\0';
        printf("%s", buffer);
    }
    close(response_pipe[0]);
    return 0;
}
No MCP client is exposed by libkreuzberg; this snippet drives the MCP CLI over stdio.