Назад к каталогу
swift-sdk

swift-sdk

Официальный

от modelcontextprotocol

0.0
0 отзывов

The official Swift SDK for Model Context Protocol servers and clients.

Описание

# MCP Swift SDK Official Swift SDK for the [Model Context Protocol][mcp] (MCP). ## Overview The Model Context Protocol (MCP) defines a standardized way for applications to communicate with AI and ML models. This Swift SDK implements both client and server components according to the [2025-03-26][mcp-spec-2025-03-26] (latest) version of the MCP specification. ## Requirements - Swift 6.0+ (Xcode 16+) See the [Platform Availability](#platform-availability) section below for platform-specific requirements. ## Installation ### Swift Package Manager Add the following to your `Package.swift` file: ```swift dependencies: [ .package(url: "https://github.com/modelcontextprotocol/swift-sdk.git", from: "0.10.0") ] ``` Then add the dependency to your target: ```swift .target( name: "YourTarget", dependencies: [ .product(name: "MCP", package: "swift-sdk") ] ) ``` ## Client Usage The client component allows your application to connect to MCP servers. ### Basic Client Setup ```swift import MCP // Initialize the client let client = Client(name: "MyApp", version: "1.0.0") // Create a transport and connect let transport = StdioTransport() let result = try await client.connect(transport: transport) // Check server capabilities if result.capabilities.tools != nil { // Server supports tools (implicitly including tool calling if the 'tools' capability object is present) } ``` > [!NOTE] > The `Client.connect(transport:)` method returns the initialization result. > This return value is discardable, > so you can ignore it if you don't need to check server capabilities. ### Transport Options for Clients #### Stdio Transport For local subprocess communication: ```swift // Create a stdio transport (simplest option) let transport = StdioTransport() try await client.connect(transport: transport) ``` #### HTTP Transport For remote server communication: ```swift // Create a streaming HTTP transport let transport = HTTPClientTransport( endpoint: URL(string: "http://localhost:8080")!, streaming: true // Enable Server-Sent Events for real-time updates ) try await client.connect(transport: transport) ``` ### Tools Tools represent functions that can be called by the client: ```swift // List available tools let (tools, cursor) = try await client.listTools() print("Available tools: \(tools.map { $0.name }.joined(separator: ", "))") // Call a tool with arguments let (content, isError) = try await client.callTool( name: "image-generator", arguments: [ "prompt": "A serene mountain landscape at sunset", "style": "photorealistic", "width": 1024, "height": 768 ] ) // Handle tool content for item in content { switch item { case .text(let text): print("Generated text: \(text)") case .image(let data, let mimeType, let metadata): if let width = metadata?["width"] as? Int, let height = metadata?["height"] as? Int { print("Generated \(width)x\(height) image of type \(mimeType)") // Save or display the image data } case .audio(let data, let mimeType): print("Received audio data of type \(mimeType)") case .resource(let uri, let mimeType, let text): print("Received resource from \(uri) of type \(mimeType)") if let text = text { print("Resource text: \(text)") } } } ``` ### Resources Resources represent data that can be accessed and potentially subscribed to: ```swift // List available resources let (resources, nextCursor) = try await client.listResources() print("Available resources: \(resources.map { $0.uri }.joined(separator: ", "))") // Read a resource let contents = try await client.readResource(uri: "resource://example") print("Resource content: \(contents)") // Subscribe to resource updates if supported if result.capabilities.resources.subscribe { try await client.subscribeToResource(uri: "resource://example") // Register notification handler await client.onNotification(ResourceUpdatedNotification.self) { message in let uri = message.params.uri print("Resource \(uri) updated with new content") // Fetch the updated resource content let updatedContents = try await client.readResource(uri: uri) print("Updated resource content received") } } ``` ### Prompts Prompts represent templated conversation starters: ```swift // List available prompts let (prompts, nextCursor) = try await client.listPrompts() print("Available prompts: \(prompts.map { $0.name }.joined(separator: ", "))") // Get a prompt with arguments let (description, messages) = try await client.getPrompt( name: "customer-service", arguments: [ "customerName": "Alice", "orderNumber": "ORD-12345", "issue": "delivery delay" ] ) // Use the prompt messages in your application print("Prompt description: \(description)") for message in messages { if case .text(tex

Отзывы (0)

Пока нет отзывов. Будьте первым!