Sponsored by Deepsite.site

MCP(Model Context Protocol) minimal Kotlin client server sample

Created By
takahirom9 months ago
Content

MCP(Model Context Protocol) minimal Kotlin client server sample

A simple weather tool demonstrating server-client interaction using the Model Context Protocol (MCP). For demonstration purposes only.

User: "What's the weather in Tokyo?"
Response: "The weather in Tokyo is sunny."

This is a Kotlin version of mcp-minimal-client-weather-server-sample

client: src/main/kotlin/Client.kt server: server.main.kts

Diagram 1: Initialization and Tool Discovery

sequenceDiagram
    autonumber

    participant User
    participant ClientApp as ClientApp (Host)<br>on Local PC
    participant LLM as LLM (e.g., Claude)<br>Remote Service
    participant MCPClient as MCPClient (Internal Component)<br>in ClientApp on Local PC
    participant MCPServer as MCPServer (e.g., Tool Server)<br>on Local PC

    Note over User, MCPServer: User starts ClientApp, initiating connection to MCPServer

    ClientApp->>+MCPClient: Instruct preparation to connect to a specific MCPServer
    Note right of ClientApp: Host manages MCPClient instances per server

    MCPClient->>+MCPServer: 1. initialize (Request)<br>[protocol_version, client_capabilities]
    Note over MCPClient, MCPServer: Start connection establishment and capability exchange (JSON-RPC)
    MCPServer-->>-MCPClient: 2. initialize (Response)<br>[selected_protocol_version, server_capabilities (tools, resources, etc.)]
    Note over MCPClient, MCPServer: Server notifies its available capabilities

    MCPClient->>MCPServer: 3. notifications/initialized (Initialization Complete Notification)
    Note over MCPClient, MCPServer: Handshake complete, normal communication possible

    MCPClient-->>-ClientApp: Initialization Success & Server Capabilities notified (implicitly via successful initialize await)
    Note right of ClientApp: Host (ClientApp) now knows the server is ready

    ClientApp->>MCPClient: 4. Instruct to get the list of tools provided by the server (session.list_tools())
    MCPClient->>+MCPServer: 5. tools/list (Request)
    Note over MCPClient, MCPServer: Request the list of tools defined on the server
    MCPServer-->>-MCPClient: 6. tools/list (Response)<br>[{"name": "get_weather", "description": "...", "inputSchema": {...}}]
    Note over MCPClient, MCPServer: Example: Returns the definition of the 'get_weather' tool

    MCPClient-->>ClientApp: 7. Notify Tool List (containing 'get_weather', etc.) via tools_response object
    Note right of ClientApp: Host stores/processes the retrieved tool information (format_tools_for_llm)

    ClientApp->>LLM: 8. Send available tool information to LLM<br>(e.g., via system prompt using formatted tools_prompt)
    Note over ClientApp, LLM: Host informs LLM that 'get_weather' is available<br>LLM can now decide to use the tool based on this info

Diagram 2: Tool Execution Flow

sequenceDiagram
    autonumber

    participant User
    participant ClientApp as ClientApp (Host)<br>on Local PC
    participant LLM as LLM (e.g., Claude)<br>Remote Service
    participant MCPClient as MCPClient (Internal Component)<br>in ClientApp on Local PC
    participant MCPServer as MCPServer (e.g., Tool Server)<br>on Local PC

    User->>ClientApp: 1. "What's the weather in Tokyo?"
    Note right of User: User asks a question that might require a tool

    ClientApp->>LLM: 2. Forward user's question with context<br>[System Prompt (with tool info) + User Question]
    Note over ClientApp, LLM: Host sends the question and formatted tool info ('get_weather') to the LLM

    LLM->>ClientApp: 3. LLM responds, requesting tool usage<br>Response: `{"tool_name": "get_weather", "arguments": {"location": "Tokyo"}}`
    Note over ClientApp, LLM: Based on provided tool info, LLM decides to use the weather tool<br>and generates the required JSON structure with arguments

    ClientApp->>User: (Optional) 4. Confirm tool execution<br>"Execute 'get_weather' on Tool Server?"
    User->>ClientApp: (Optional) 5. "Yes"
    Note over ClientApp, User: For security/transparency, host might seek user permission (Not implemented in the sample code)

    ClientApp->>+MCPClient: 6. Instruct execution of 'get_weather' tool (session.call_tool())<br>Arguments: {"location": "Tokyo"}
    Note right of ClientApp: Host instructs the MCP server via MCPClient

    MCPClient->>+MCPServer: 7. tools/call (Request)<br>[name: "get_weather", arguments: {"location": "Tokyo"}]
    Note over MCPClient, MCPServer: Invoke server function with specified tool name and arguments (JSON-RPC)

    MCPServer->>MCPServer: Internal processing (executes get_weather function)
    Note over MCPServer: Logs execution, returns result (e.g., "Sunny")
    MCPServer-->>-MCPClient: 8. tools/call (Response)<br>[result: {content: [{type: "text", text: "Sunny"}]}]
    Note over MCPClient, MCPServer: Server returns the processing result (weather info) in structured format

    MCPClient-->>-ClientApp: 9. Notify Tool Execution Result<br>Result object contains: "Sunny" (extracted from tool_result_obj.content[0].text)

    ClientApp->>LLM: 10. Send tool execution result back to LLM<br>New User Message: "Tool 'get_weather' returned: 'Sunny'. Answer the original question based on this."
    Note over ClientApp, LLM: Host feeds back the information obtained from the server to the LLM

    LLM->>ClientApp: 11. Generate final response (as text)<br>"The weather in Tokyo is Sunny."

    ClientApp->>User: 12. Display the final response generated by LLM
    Note right of ClientApp: Present the final answer to the user
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
CursorThe AI Code Editor
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Playwright McpPlaywright MCP server
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Amap Maps高德地图官方 MCP Server
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
WindsurfThe new purpose-built IDE to harness magic
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
DeepChatYour AI Partner on Desktop
ChatWiseThe second fastest AI chatbot™
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
Serper MCP ServerA Serper MCP Server
Tavily Mcp