|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "metadata": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "source": "# MCP Client Example" |
| 7 | + }, |
| 8 | + { |
| 9 | + "metadata": { |
| 10 | + "ExecuteTime": { |
| 11 | + "end_time": "2025-11-12T19:51:02.127574Z", |
| 12 | + "start_time": "2025-11-12T19:51:01.088739Z" |
| 13 | + } |
| 14 | + }, |
| 15 | + "cell_type": "code", |
| 16 | + "source": [ |
| 17 | + "%useLatestDescriptors\n", |
| 18 | + "%use coroutines\n", |
| 19 | + "\n", |
| 20 | + "USE {\n", |
| 21 | + " dependencies(\n", |
| 22 | + " \"io.modelcontextprotocol:kotlin-sdk-client-jvm:0.7.7\",\n", |
| 23 | + " \"io.kotest:kotest-assertions-core-jvm:6.0.4\",\n", |
| 24 | + " \"io.ktor:ktor-client-core-jvm:3.2.3\",\n", |
| 25 | + " \"io.ktor:ktor-client-logging-jvm:3.2.3\",\n", |
| 26 | + " \"io.ktor:ktor-client-apache5-jvm:3.2.3\",\n", |
| 27 | + " \"org.slf4j:slf4j-simple:2.0.17\",\n", |
| 28 | + " )\n", |
| 29 | + "}" |
| 30 | + ], |
| 31 | + "outputs": [], |
| 32 | + "execution_count": 1 |
| 33 | + }, |
| 34 | + { |
| 35 | + "metadata": { |
| 36 | + "ExecuteTime": { |
| 37 | + "end_time": "2025-11-12T19:51:03.320450Z", |
| 38 | + "start_time": "2025-11-12T19:51:02.132357Z" |
| 39 | + } |
| 40 | + }, |
| 41 | + "cell_type": "code", |
| 42 | + "source": [ |
| 43 | + "import kotlinx.coroutines.runBlocking\n", |
| 44 | + "import io.modelcontextprotocol.kotlin.sdk.EmptyJsonObject\n", |
| 45 | + "import io.modelcontextprotocol.kotlin.sdk.Implementation\n", |
| 46 | + "import io.modelcontextprotocol.kotlin.sdk.TextContent\n", |
| 47 | + "import io.modelcontextprotocol.kotlin.sdk.client.Client\n", |
| 48 | + "import io.modelcontextprotocol.kotlin.sdk.client.ClientOptions\n", |
| 49 | + "import io.modelcontextprotocol.kotlin.sdk.client.StdioClientTransport\n", |
| 50 | + "import io.modelcontextprotocol.kotlin.sdk.client.StreamableHttpClientTransport\n", |
| 51 | + "import io.ktor.client.HttpClient\n", |
| 52 | + "import io.ktor.client.call.body\n", |
| 53 | + "import io.ktor.client.request.get\n", |
| 54 | + "import kotlinx.serialization.Serializable\n", |
| 55 | + "import kotlinx.serialization.json.JsonObject\n", |
| 56 | + "import io.ktor.client.plugins.logging.*\n", |
| 57 | + "import io.ktor.client.plugins.sse.SSE\n", |
| 58 | + "\n", |
| 59 | + "runBlocking {\n", |
| 60 | + " val client = Client(\n", |
| 61 | + " clientInfo = Implementation(\n", |
| 62 | + " name = \"my-client\",\n", |
| 63 | + " version = \"1.0.0\"\n", |
| 64 | + " ),\n", |
| 65 | + " options = ClientOptions(),\n", |
| 66 | + " )\n", |
| 67 | + "\n", |
| 68 | + " client.connect(\n", |
| 69 | + " StreamableHttpClientTransport(\n", |
| 70 | + " client = HttpClient {\n", |
| 71 | + " install(SSE)\n", |
| 72 | + " install(Logging) {\n", |
| 73 | + " level = LogLevel.ALL\n", |
| 74 | + " }\n", |
| 75 | + " },\n", |
| 76 | + " url = \"https://learn.microsoft.com/api/mcp\"\n", |
| 77 | + " )\n", |
| 78 | + " )\n", |
| 79 | + " client.ping()\n", |
| 80 | + "\n", |
| 81 | + " val listToolsResult = client.listTools()\n", |
| 82 | + " println(listToolsResult.tools)\n", |
| 83 | + "}" |
| 84 | + ], |
| 85 | + "outputs": [ |
| 86 | + { |
| 87 | + "name": "stdout", |
| 88 | + "output_type": "stream", |
| 89 | + "text": [ |
| 90 | + "[Tool(name=microsoft_docs_search, title=Microsoft Docs Search, description=Search official Microsoft/Azure documentation to find the most relevant and trustworthy content for a user's query. This tool returns up to 10 high-quality content chunks (each max 500 tokens), extracted from Microsoft Learn and other official sources. Each result includes the article title, URL, and a self-contained content excerpt optimized for fast retrieval and reasoning. Always use this tool to quickly ground your answers in accurate, first-party Microsoft/Azure knowledge.\n", |
| 91 | + "\n", |
| 92 | + "The `question` parameter is no longer used, use `query` instead.\n", |
| 93 | + "\n", |
| 94 | + "## Follow-up Pattern\n", |
| 95 | + "To ensure completeness, use microsoft_docs_fetch when high-value pages are identified by search. The fetch tool complements search by providing the full detail. This is a required step for comprehensive results., inputSchema=Input(properties={\"query\":{\"description\":\"a query or topic about Microsoft/Azure products, services, platforms, developer tools, frameworks, or APIs\",\"type\":\"string\",\"default\":null},\"question\":{\"description\":\"this parameter is no longer used, use query instead.\",\"type\":\"string\",\"default\":null}}, required=null), outputSchema=null, annotations=ToolAnnotations(title=Microsoft Docs Search, readOnlyHint=true, destructiveHint=false, idempotentHint=true, openWorldHint=true), _meta={}), Tool(name=microsoft_code_sample_search, title=Microsoft Code Sample Search, description=Search for code snippets and examples in official Microsoft Learn documentation. This tool retrieves relevant code samples from Microsoft documentation pages providing developers with practical implementation examples and best practices for Microsoft/Azure products and services related coding tasks. This tool will help you use the **LATEST OFFICIAL** code snippets to empower coding capabilities.\n", |
| 96 | + "\n", |
| 97 | + "## When to Use This Tool\n", |
| 98 | + "- When you are going to provide sample Microsoft/Azure related code snippets in your answers.\n", |
| 99 | + "- When you are **generating any Microsoft/Azure related code**.\n", |
| 100 | + "\n", |
| 101 | + "## Usage Pattern\n", |
| 102 | + "Input a descriptive query, or SDK/class/method name to retrieve related code samples. The optional parameter `language` can help to filter results.\n", |
| 103 | + "\n", |
| 104 | + "Eligible values for `language` parameter include: csharp javascript typescript python powershell azurecli al sql java kusto cpp go rust ruby php, inputSchema=Input(properties={\"query\":{\"description\":\"a descriptive query, SDK name, method name or code snippet related to Microsoft/Azure products, services, platforms, developer tools, frameworks, APIs or SDKs\",\"type\":\"string\"},\"language\":{\"description\":\"Optional parameter specifying the programming language of code snippets to retrieve. Can significantly improve search quality if provided. Eligible values: csharp javascript typescript python powershell azurecli al sql java kusto cpp go rust ruby php\",\"type\":\"string\",\"default\":null}}, required=[query]), outputSchema=null, annotations=ToolAnnotations(title=Microsoft Code Sample Search, readOnlyHint=true, destructiveHint=false, idempotentHint=true, openWorldHint=true), _meta={}), Tool(name=microsoft_docs_fetch, title=Microsoft Docs Fetch, description=Fetch and convert a Microsoft Learn documentation page to markdown format. This tool retrieves the latest complete content of Microsoft documentation pages including Azure, .NET, Microsoft 365, and other Microsoft technologies.\n", |
| 105 | + "\n", |
| 106 | + "## When to Use This Tool\n", |
| 107 | + "- When search results provide incomplete information or truncated content\n", |
| 108 | + "- When you need complete step-by-step procedures or tutorials\n", |
| 109 | + "- When you need troubleshooting sections, prerequisites, or detailed explanations\n", |
| 110 | + "- When search results reference a specific page that seems highly relevant\n", |
| 111 | + "- For comprehensive guides that require full context\n", |
| 112 | + "\n", |
| 113 | + "## Usage Pattern\n", |
| 114 | + "Use this tool AFTER microsoft_docs_search when you identify specific high-value pages that need complete content. The search tool gives you an overview; this tool gives you the complete picture.\n", |
| 115 | + "\n", |
| 116 | + "## URL Requirements\n", |
| 117 | + "- The URL must be a valid link from the microsoft.com domain.\n", |
| 118 | + "\n", |
| 119 | + "## Output Format\n", |
| 120 | + "markdown with headings, code blocks, tables, and links preserved., inputSchema=Input(properties={\"url\":{\"description\":\"URL of the Microsoft documentation page to read\",\"type\":\"string\"}}, required=[url]), outputSchema=null, annotations=ToolAnnotations(title=Microsoft Docs Fetch, readOnlyHint=true, destructiveHint=false, idempotentHint=true, openWorldHint=true), _meta={})]\n" |
| 121 | + ] |
| 122 | + } |
| 123 | + ], |
| 124 | + "execution_count": 2 |
| 125 | + } |
| 126 | + ], |
| 127 | + "metadata": { |
| 128 | + "kernelspec": { |
| 129 | + "display_name": "Kotlin", |
| 130 | + "language": "kotlin", |
| 131 | + "name": "kotlin" |
| 132 | + }, |
| 133 | + "language_info": { |
| 134 | + "name": "kotlin", |
| 135 | + "version": "2.2.20-Beta2", |
| 136 | + "mimetype": "text/x-kotlin", |
| 137 | + "file_extension": ".kt", |
| 138 | + "pygments_lexer": "kotlin", |
| 139 | + "codemirror_mode": "text/x-kotlin", |
| 140 | + "nbconvert_exporter": "" |
| 141 | + } |
| 142 | + }, |
| 143 | + "nbformat": 4, |
| 144 | + "nbformat_minor": 0 |
| 145 | +} |
0 commit comments