MCP Kotlin SDK 指南

MCP Kotlin SDK

本文档是模型上下文协议(Model Context Protocol,MCP)的Kotlin实现方案,提供与LLM界面集成的客户端和服务端能力。

概述

模型上下文协议允许应用程序以标准化方式为LLM提供上下文,将上下文提供与实际LLM交互分离。本Kotlin SDK完整实现了MCP规范,可轻松实现:

  • 构建可连接任何MCP服务端的客户端
  • 创建暴露资源、提示和工具的MCP服务端
  • 使用标准传输协议如stdio、SSE和WebSocket
  • 处理所有MCP协议消息和生命周期事件

示例项目

  • kotlin-mcp-server: 演示如何设置包含不同工具和功能的Kotlin MCP服务端
  • weather-stdio-server: 展示如何使用STDIO传输构建提供天气预报和警报的MCP服务端
  • kotlin-mcp-client: 演示构建通过STDIO连接MCP服务端并与Anthropic API集成的交互式客户端

安装

在构建文件中添加仓库配置:

repositories {
    mavenCentral()
}

添加依赖项:

dependencies {
    implementation("io.modelcontextprotocol:kotlin-sdk:0.3.0")
}

快速入门

创建客户端

import io.modelcontextprotocol.kotlin.sdk.client.Client
import io.modelcontextprotocol.kotlin.sdk.client.StdioClientTransport
import io.modelcontextprotocol.kotlin.sdk.Implementation

val client = Client(
    clientInfo = Implementation(
        name = "example-client",
        version = "1.0.0"
    )
)

val transport = StdioClientTransport(
    inputStream = processInputStream,
    outputStream = processOutputStream
)

// 连接服务端
client.connect(transport)

// 列出可用资源
val resources = client.listResources()

// 读取特定资源
val resourceContent = client.readResource(
    ReadResourceRequest(uri = "file:///example.txt")
)

创建服务端

import io.modelcontextprotocol.kotlin.sdk.server.Server
import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions
import io.modelcontextprotocol.kotlin.sdk.server.StdioServerTransport
import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities

val server = Server(
    serverInfo = Implementation(
        name = "example-server",
        version = "1.0.0"
    ),
    options = ServerOptions(
        capabilities = ServerCapabilities(
            resources = ServerCapabilities.Resources(
                subscribe = true,
                listChanged = true
            )
        )
    )
)

// 添加资源
server.addResource(
    uri = "file:///example.txt",
    name = "Example Resource",
    description = "An example text file",
    mimeType = "text/plain"
) { request ->
    ReadResourceResult(
        contents = listOf(
            TextResourceContents(
                text = "This is the content of the example resource.",
                uri = request.uri,
                mimeType = "text/plain"
            )
        )
    )
}

// 使用STDIO传输启动服务端
val transport = StdioServerTransport()
server.connect(transport)

使用SSE传输

在Ktor的Application中直接使用:

import io.ktor.server.application.*
import io.modelcontextprotocol.kotlin.sdk.server.mcp

fun Application.module() {
    mcp {
        Server(
            serverInfo = Implementation(
                name = "example-sse-server",
                version = "1.0.0"
            ),
            options = ServerOptions(
                capabilities = ServerCapabilities(
                    prompts = ServerCapabilities.Prompts(listChanged = null),
                    resources = ServerCapabilities.Resources(subscribe = null, listChanged = null)
                )
            )
        )
    }
}

在自定义Ktor路由中使用:

import io.ktor.server.application.*
import io.ktor.server.sse.SSE
import io.modelcontextprotocol.kotlin.sdk.server.mcp

fun Application.module() {
    install(SSE)

    routing {
        route("myRoute") {
            mcp {
                Server(
                    serverInfo = Implementation(
                        name = "example-sse-server",
                        version = "1.0.0"
                    ),
                    options = ServerOptions(
                        capabilities = ServerCapabilities(
                            prompts = ServerCapabilities.Prompts(listChanged = null),
                            resources = ServerCapabilities.Resources(subscribe = null, listChanged = null)
                        )
                    )
                )
            }
        }
    }
}

贡献指南

贡献前请阅读贡献指南行为准则

许可证

本项目采用MIT许可证,详见LICENSE文件。