{"id":51892050,"url":"https://github.com/1amageek/swift-context-protocol","last_synced_at":"2026-07-26T05:01:42.465Z","repository":{"id":278016160,"uuid":"934248149","full_name":"1amageek/swift-context-protocol","owner":"1amageek","description":"swift-context-protocol is a Swift-based implementation of the Model Context Protocol (MCP) for AI contexts. It leverages Swift’s distributed actor model to enable type-safe, asynchronous remote invocation of tools, resources, and prompts.","archived":false,"fork":false,"pushed_at":"2025-02-18T03:13:00.000Z","size":22,"stargazers_count":11,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-03-03T00:34:15.787Z","etag":null,"topics":["ai","modelcontextprotocol","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/1amageek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-17T14:15:29.000Z","updated_at":"2026-03-02T02:29:27.000Z","dependencies_parsed_at":"2025-02-17T15:37:14.539Z","dependency_job_id":null,"html_url":"https://github.com/1amageek/swift-context-protocol","commit_stats":null,"previous_names":["1amageek/swift-context-protocol"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/1amageek/swift-context-protocol","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1amageek%2Fswift-context-protocol","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1amageek%2Fswift-context-protocol/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1amageek%2Fswift-context-protocol/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1amageek%2Fswift-context-protocol/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/1amageek","download_url":"https://codeload.github.com/1amageek/swift-context-protocol/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1amageek%2Fswift-context-protocol/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35901460,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-07-20T02:08:10.276Z","status":"online","status_checked_at":"2026-07-26T02:00:06.503Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","modelcontextprotocol","swift"],"created_at":"2026-07-26T05:01:42.393Z","updated_at":"2026-07-26T05:01:42.458Z","avatar_url":"https://github.com/1amageek.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# swift-context-protocol\n\n[![Swift Version](https://img.shields.io/badge/Swift-6.0%2B-blue.svg)](https://swift.org)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\n_A distributed context protocol implementation for Swift._\n\nswift-context-protocol is a Swift-based implementation of the Model Context Protocol (MCP) for AI contexts. It leverages Swift’s distributed actor model to enable type-safe, asynchronous remote invocation of tools, resources, and prompts.\n\n## Features\n\n- **Distributed Context Protocol**  \n  Define a common interface (`ContextProtocol`) for operations like ping, initialize, complete, set logging level, resource reading, tool calling, and prompt execution.\n  \n- **Tool, Resource, and Prompt Protocols**  \n  Provide standardized protocols for:\n  - **Tool**: Executes a specific operation using JSON schema validated inputs.\n  - **Resource**: Represents a resource identified by a URI, supporting asynchronous read operations.\n  - **Prompt**: Generates prompt templates or responses based on input parameters.\n\n- **JSON Schema Support**  \n  Input and output formats can be validated and described using JSONSchema.\n\n- **Distributed Actors**  \n  Leverages Swift’s new distributed actor model (with `WebSocketActorSystem`) to allow remote invocation of protocol methods.\n\n- **Capability Negotiation \u0026 Initialization**  \n  Exchange client and server information along with supported capabilities during the initialization handshake.\n\n## Installation\n\nAdd the following dependency to your `Package.swift`:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/1amageek/swift-context-protocol.git\", from: \"1.0.0\")\n]\n```\n\nThen add `\"swift-context-protocol\"` to your target dependencies.\n\n## Usage\n\n### Creating a Custom Tool\n\nImplement the `Tool` protocol to create a custom tool. For example, here's a simple echo tool that returns the input string:\n\n```swift\nimport Foundation\nimport JSONSchema\nimport ContextProtocol\n\n/// A simple tool that echoes the provided input.\npublic struct EchoTool: Tool {\n    \n    public typealias Input = Envelop\u003cString\u003e\n    public typealias Output = String\n    \n    public var name: String = \"echo\"\n    public var description: String = \"A tool that echoes the provided input.\"\n    public var inputSchema: JSONSchema? = .string()\n    \n    public var guide: String? = \"\"\"\n    # Tool Name\n    EchoTool\n    \n    ## Description\n    This tool echoes back the input string provided to it.\n    \n    ## Parameters\n    - `input`: The string to be echoed.\n      - **Type**: `String`\n      - **Description**: The input text that will be returned as output.\n    \n    ## Usage\n    Provide a valid string as input.\n    \n    ## Example\n    ```swift\n    let input = \"Hello, world!\"\n    // EchoTool returns: \"Hello, world!\"\n    ```\n    \"\"\"\n    \n    public func run(_ input: Envelop\u003cString\u003e) async throws -\u003e String {\n        return input.data\n    }\n}\n```\n\n### Registering Components and Starting the Server\n\nCreate a distributed actor (e.g., `ContextActor`) that implements the `ContextProtocol` and registers your tools, resources, and prompts.\n\n```swift\nimport Foundation\nimport Distributed\nimport WebSocketActors\nimport ContextProtocol\n\n@main\nstruct Boot {\n    \n    static func main() async throws {\n        let server = ContextServer()\n        server.setTools([ EchoTool() ])\n        try await server.start()\n    }\n}\n```\n\n### Connecting a Client\n\nYou can connect to the server using a client wrapper like `ContextClient`:\n\n```swift\nimport Foundation\nimport Distributed\nimport ContextProtocol\n\n@main\nstruct ClientBoot {\n    static func main() async throws {\n        let client = try await ContextClient.connectServer()\n        let data = Envelop(data: \"Hello, World!\")\n        let parameters = try JSONEncoder().encode(data)\n        let toolResponse = try await client.session.callTool(\n            name: \"echo\",\n            parameters: parameters\n        )\n        print(toolResponse)\n    }\n}\n```\n\nDuring connection, the client performs an initialization handshake where it sends its information and capabilities and receives server details and instructions. A cool connection banner is printed on success.\n\n## API Overview\n\n### ContextProtocol\n\nThe distributed actor protocol defines methods for:\n\n- **initialize**: Handshake with client info and capabilities.\n- **ping**: Simple connectivity check.\n- **complete**: Trigger completion logic.\n- **setLoggingLevel**: Adjust logging level.\n- **getPrompt**: Request a prompt.\n- **listPrompts**: List available prompt templates.\n- **listResources**: Retrieve a list of resources.\n- **readResource**: Read resource content (returns a `ResourceContentData` enum).\n- **subscribeResource / unsubscribeResource**: Manage resource subscriptions.\n- **callTool**: Call a tool and receive its output as a `ResourceContentData` enum.\n- **listTools**: Retrieve a list of tools.\n- **sendRootsListChanged**: Notify changes in the root resource list.\n\n### Domain Types\n\n- **ServerInfo / ClientInfo**: Exchange basic identity and versioning information.\n- **CapabilityConfig**: Key-value settings indicating supported capabilities.\n- **ResourceContentData**: An enum (e.g., `.text(String)` or `.binary(Data)`) to represent resource contents.\n- **ToolResponse / PromptResponse / ResourceResponse**: DTOs used to list available tools, prompts, and resources.\n- **ListResponse\\\u003cT\\\u003e**: A generic wrapper for lists.\n\n## Contributing\n\nContributions are welcome! Please open issues or pull requests on GitHub.\n\n## License\n\nThis project is licensed under the MIT License.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1amageek%2Fswift-context-protocol","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F1amageek%2Fswift-context-protocol","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1amageek%2Fswift-context-protocol/lists"}