Token导航 LogoToken导航TokenDH.com
MCP Glib logo
AI代理未说明官方级别未说明来源级核验

MCP Glib

MCP Server

mcp-glib是一个基于GLib/GObject的Model Context Protocol (MCP)实现,用于在C语言中构建MCP服务器和客户端,支持多种传输协议和异步操作。

工具数

6

提示词数

0

GitHub Stars

0

资源数

0
服务器开发本地部署AI代理

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

copyleft-games

提供方

copyleft-games

最后核验

2026/5/17 20:22

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

详细介绍

mcp glib

GLib/GObject的实现 模型上下文协议(MCP) 用于在C中构建MCP服务器和客户端。

特性

  • MCP服务器支持 -向人工智能应用程序公开工具、资源和提示
  • MCP客户端支持 -连接到MCP服务器并与之交互
  • 任务API -对长时间运行的工具操作的实验支持
  • 标准运输 -通过stdin/stdout进行基于进程的通信
  • G对象反思 -对语言绑定(Python、JavaScript等)的一流支持
  • 异步操作 -基于libdex构建,用于可组合异步期货
  • 交叉平台的 -通过mingw交叉编译的Windows DLL

状态

完成 -完整的MCP 2025-03-26协议实施,包括:

  • 工具、资源、资源模板和提示
  • 具有异步操作的服务器和客户端类
  • 为长时间运行的操作任务API(实验)
  • 带有子进程生成的Stdio传输
  • 用于语言绑定的GIR/typelib生成
  • 综合测试套件(170多项测试)

需求

  • 支持gnu89的GCC
  • GLib 2.0/GObject 2.0/GIO 2.0/GIO Unix 2.0
  • json-glib 1.0
  • libsoup 3.0
  • Libdex 1.0

Fedora 43

sudo dnf install gcc make pkgconfig glib2-devel json-glib-devel \
    libsoup3-devel libdex-devel gobject-introspection-devel

建筑

make              # Build library
make test         # Run tests
make examples     # Build examples
make gir          # Generate GIR and typelib
make install      # Install to /usr/local

交叉编译

make WINDOWS=1      # Windows x64 DLL (via mingw)
make LINUX_ARM64=1  # Linux ARM64

docs/building.md 了解详情。

快速示例

简单MCP服务器

#include 

static GMainLoop *main_loop = NULL;

/* Tool handler - called when client invokes the tool */
static McpToolResult *
echo_handler (McpServer  *server,
              const gchar *name,
              JsonObject  *arguments,
              gpointer     user_data)
{
    McpToolResult *result;
    const gchar *message = NULL;

    if (arguments != NULL && json_object_has_member (arguments, "message"))
    {
        message = json_object_get_string_member (arguments, "message");
    }

    if (message == NULL)
    {
        message = "(no message)";
    }

    result = mcp_tool_result_new (FALSE);
    mcp_tool_result_add_text (result, message);

    return result;
}

static void
on_client_disconnected (McpServer *server, gpointer user_data)
{
    g_main_loop_quit (main_loop);
}

int
main (int argc, char *argv[])
{
    g_autoptr(McpServer) server = NULL;
    g_autoptr(McpStdioTransport) transport = NULL;
    g_autoptr(McpTool) echo_tool = NULL;

    main_loop = g_main_loop_new (NULL, FALSE);

    /* Create server */
    server = mcp_server_new ("my-server", "1.0.0");
    mcp_server_set_instructions (server, "Use the echo tool to echo messages.");

    g_signal_connect (server, "client-disconnected",
                      G_CALLBACK (on_client_disconnected), NULL);

    /* Add a tool */
    echo_tool = mcp_tool_new ("echo", "Echoes back the message");
    mcp_server_add_tool (server, echo_tool, echo_handler, NULL, NULL);

    /* Create stdio transport and start */
    transport = mcp_stdio_transport_new ();
    mcp_server_set_transport (server, MCP_TRANSPORT (transport));
    mcp_server_start_async (server, NULL, NULL, NULL);

    g_main_loop_run (main_loop);
    g_main_loop_unref (main_loop);

    return 0;
}

简单MCP客户端

#include 

static GMainLoop *main_loop = NULL;
static McpClient *client = NULL;

static void
on_tool_called (GObject      *source,
                GAsyncResult *result,
                gpointer      user_data)
{
    g_autoptr(GError) error = NULL;
    McpToolResult *tool_result;

    tool_result = mcp_client_call_tool_finish (MCP_CLIENT (source), result, &error);

    if (error != NULL)
    {
        g_printerr ("Tool call failed: %s\n", error->message);
    }
    else
    {
        JsonArray *content = mcp_tool_result_get_content (tool_result);
        guint i;

        for (i = 0; i message);
        g_main_loop_quit (main_loop);
        return;
    }

    g_print ("Connected!\n");

    /* Call the echo tool */
    args = json_object_new ();
    json_object_set_string_member (args, "message", "Hello, MCP!");
    mcp_client_call_tool_async (client, "echo", args, NULL, on_tool_called, NULL);
}

int
main (int argc, char *argv[])
{
    g_autoptr(McpStdioTransport) transport = NULL;
    g_autoptr(GError) error = NULL;

    main_loop = g_main_loop_new (NULL, FALSE);

    /* Create client */
    client = mcp_client_new ("my-client", "1.0.0");

    /* Spawn server as subprocess */
    transport = mcp_stdio_transport_new_subprocess_simple ("./my-server", &error);
    if (transport == NULL)
    {
        g_printerr ("Failed to spawn server: %s\n", error->message);
        return 1;
    }

    mcp_client_set_transport (client, MCP_TRANSPORT (transport));
    mcp_client_connect_async (client, NULL, on_connected, NULL);

    g_main_loop_run (main_loop);

    g_object_unref (client);
    g_main_loop_unref (main_loop);

    return 0;
}

例子

examples/ 目录包含工作示例程序:

  • 简单服务器.c -带有echo工具、自述文件资源和问候提示的最小服务器
  • 简单客户端.c -连接到服务器并执行所有功能的客户端
  • 计算器服务器.c -计算器加,减,乘,除,sqrt,电动工具
  • 文件系统服务器.c -使用资源模板从目录中提供文件

构建和运行示例:

make examples
./build/simple-client ./build/simple-server
./build/simple-client ./build/calculator-server
./build/simple-client ./build/filesystem-server /path/to/directory

CLI工具

用于与MCP服务器交互的命令行工具:

工具说明
mcp-inspect显示服务器信息、功能、工具、资源、提示
mcp-call使用JSON参数调用特定工具
mcp-read按URI读取资源
mcp-prompt获取带有参数的提示
mcp-shell用于探索服务器的交互式REPL
mcp-remote-client将stdio代理到远程HTTP/WebSocket MCP服务器

构建和使用:

# Build tools (requires readline-devel for mcp-shell)
make tools

# Inspect a server
./build/mcp-inspect --stdio ./build/simple-server

# Call a tool
./build/mcp-call --stdio ./build/calculator-server -- add '{"a": 5, "b": 3}'

# Interactive shell
./build/mcp-shell --stdio ./build/simple-server

# Bridge stdio to a remote HTTP server
./build/mcp-remote-client --http https://api.example.com/mcp

docs/cli-tools.md 以获取完整的文档。

测试

运行测试套件:

make test

测试包括:

  • 所有核心类型(枚举、内容、工具、资源、提示、任务)的单元测试
  • 消息编码/解码的协议测试
  • 使用模拟传输进行服务器和客户端测试
  • 与链接传输对的集成测试

API概述

核心类型

类型描述
McpServerMCP服务器-公开工具、资源和提示
McpClientMCP客户端-连接到服务器
McpTool使用JSON模式的工具定义
McpResource资源定义(URI、名称、mime类型)
McpResourceTemplate动态资源的URI模板
McpPrompt带参数的提示模板
McpTask长期运行(实验)

运输类型

类型描述
McpTransport抽象传输接口
McpStdioTransport基于标准的运输

结果类型(框内)

类型描述
McpToolResult工具调用的结果
McpResourceContents从资源读取内容
McpPromptResult提示获取结果
McpPromptMessage提示结果中的消息

语言绑定

生成GObject自检文件:

make gir

这将创建:

  • build/Mcp-1.0.gir -GIR XML文件
  • build/Mcp-1.0.typelib -编译的typelib

Python示例

import gi
gi.require_version('Mcp', '1.0')
from gi.repository import Mcp, GLib

# Create a server
server = Mcp.Server.new("python-server", "1.0.0")
# ... add tools, resources, prompts

许可证

AGPL-3.0或更高版本

版权所有(C)2025 Copyleft Games

文档

docs/ 详细文档:

参考文献

目录标签

目录标签

服务器开发本地部署AI代理CMCP协议GLib/GObject客户端开发异步操作

接入字段

传输方式(transport,传输协议)

未说明

鉴权方式(authType,认证方式)

none

工具数量(toolCount,工具数)

6

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

未说明none部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

仍需确认:installCommand

来源信息

继续浏览同类 MCP