MCP-UI协议SDK

一个用于MCP-UI协议的Go语言SDK,支持服务器生成交互式UI资源并在沙盒iframe中渲染。

  • by ironystock
  • Go
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 ironystock/mcpui-go

使用说明

mcpui走

MCP-UI协议的Go SDK,使MCP服务器能够返回客户端可以在沙盒iframe中呈现的交互式UI资源。

概述

MCP-UI扩展了模型上下文协议(MCP),以支持丰富的交互式用户界面。此SDK为基于Go的MCP服务器提供了服务器端实现,允许它们:

  • 使用HTML、URL或远程DOM内容生成UI资源
  • 处理来自UI组件的用户交互
  • 将响应发送回UI层
  • 将操作路由到适当的处理程序

安装

go get github.com/ironystock/mcpui-go@v0.1.0

快速开始

创建简单的HTML资源

package main

import (
    "encoding/json"
    "fmt"

    "github.com/ironystock/mcpui-go"
)

func main() {
    // Create HTML content
    content := &mcpui.HTMLContent{
        HTML: `<div style="padding: 20px; font-family: sans-serif;">
            <h1>Hello, World!</h1>
            <p>This is rendered in a sandboxed iframe.</p>
        </div>`,
    }

    // Create resource contents for MCP response
    rc, _ := mcpui.NewUIResourceContents("ui://greeting/hello", content)

    data, _ := json.MarshalIndent(rc, "", "  ")
    fmt.Println(string(data))
}

处理UI操作

package main

import (
    "context"
    "fmt"

    "github.com/ironystock/mcpui-go"
)

func main() {
    router := mcpui.NewRouter()

    // Handle tool actions
    router.HandleType(mcpui.ActionTypeTool, mcpui.WrapToolHandler(
        func(ctx context.Context, toolName string, params map[string]any) (any, error) {
            fmt.Printf("Tool called: %s\n", toolName)
            return map[string]string{"status": "executed"}, nil
        },
    ))

    // Handle specific resource
    router.HandleResource("ui://dashboard/main",
        func(ctx context.Context, req *mcpui.UIActionRequest) (*mcpui.UIActionResult, error) {
            return &mcpui.UIActionResult{Response: "dashboard handled"}, nil
        },
    )

    fmt.Println("Router configured")
}

内容类型

SDK支持三种类型的UI内容:

类型 描述 用例
HTMLContent 通过iframe srcdoc呈现的内联HTML 简单、自包含的UI
URLContent 通过iframe src呈现的外部URL 现有web应用程序、复杂UI
RemoteDOMContent 使用远程DOM的基于脚本的UI 基于框架的动态UI

HTML内容

最适合简单、自包含的UI:

content := &mcpui.HTMLContent{
    HTML: `<div>
        <h1>Status Dashboard</h1>
        <p>All systems operational</p>
    </div>`,
}

URL内容

最适合现有的web应用程序:

content := &mcpui.URLContent{
    URL: "https://example.com/dashboard",
}

远程DOM内容

最适合动态、基于框架的UI:

content := &mcpui.RemoteDOMContent{
    Script:    "React.createElement('div', null, 'Hello from React!')",
    Framework: mcpui.FrameworkReact,
}

行动处理

当用户与UI资源交互时,客户端发送 UIAction 信息。SDK提供了一个灵活的路由系统:

router := mcpui.NewRouter()

// Handle by action type
router.HandleType(mcpui.ActionTypeTool, toolHandler)
router.HandleType(mcpui.ActionTypePrompt, promptHandler)
router.HandleType(mcpui.ActionTypeResource, resourceHandler)

// Handle by specific resource URI
router.HandleResource("ui://audio/mixer", audioHandler)
router.HandleResource("ui://scene/preview", sceneHandler)

// Route an action
result, err := router.Route(ctx, &mcpui.UIActionRequest{
    SourceURI: "ui://audio/mixer",
    Action:    action,
})

响应消息

将响应发送回UI:

// Acknowledge receipt
ack := mcpui.NewReceivedResponse(action.MessageID)

// Success response
resp := mcpui.NewSuccessResponse(action.MessageID, result)

// Error response
errResp := mcpui.NewErrorResponse(action.MessageID, err)

文档

  • 内容类型 -HTMLContent、URLContent、RemoteDOMContent
  • 资源 -UIResource、UIResource内容、验证
  • 行动 -UIAction类型、有效载荷、解析
  • 回复 -UI响应构建器,成功/错误处理
  • 处理器 -UIActionHandler、路由器、类型化包装器
  • 整合 -如何与MCP服务器集成

例子

看 示例 完整、可运行示例的目录:

许可证

MIT许可证-请参阅 许可证 了解详情。

相关