{"slug": "building-ai-agents-with-the-kotlin-agent-development-kit-adk", "title": "Building AI Agents with the Kotlin Agent Development Kit (ADK)", "summary": "A developer built a starter 'Hello World' AI agent using Kotlin and Google's native Kotlin Agent Development Kit (ADK). The agent uses Gemini to decide when to call a tool discovered from a local Kotlin Model Context Protocol (MCP) server, showcasing static typing benefits for agent configuration.", "body_md": "This tutorial builds a starter \"Hello World\" style agent using Kotlin and the native Kotlin version of the Agent Development Kit (ADK).\n\nThe full sample project is available on GitHub:\n\nThis project is a runnable Kotlin Agent Development Kit (ADK) demo. A Kotlin\n`LlmAgent`\n\nuses Gemini to decide when to call a `greet`\n\ntool discovered from a\nlocal Kotlin Model Context Protocol (MCP) server.\n\nThe project has two Gradle modules:\n\n`agent`\n\n: the Kotlin ADK agent, Gemini model configuration, MCP toolset, and\ninteractive `ReplRunner`\n\n;`server`\n\n: the Ktor MCP server that exposes `greet`\n\n.`com.google.adk:google-adk-kotlin-core`\n\n(v0.6.0)`io.modelcontextprotocol:kotlin-sdk-jvm`\n\n(v0.8.1)The Gradle wrapper is included.\n\nCreate the local environment file:\n\n```\ncp .env.example .env\n```\n\nSet `GOOGLE_API_KEY`\n\nin `.env`\n\n, then load it:\n\n```\nsource ./set_env.sh\n```\n\nThe file is ignored by Git.\n\nStart the Kotlin MCP server in one…\n\nKotlin is a modern, statically typed programming language created by JetBrains. It runs on the Java Virtual Machine (JVM), works alongside existing Java libraries, and is widely used for Android, backend, and multiplatform development.\n\nStatic typing is especially useful when building agents. Agent configuration, tool schemas, and tool results can all be checked by the compiler before a prompt reaches the model.\n\nThis sample uses **Java 25**. If Java is not installed, [SDKMAN!](https://sdkman.io/) is a convenient way to install and switch between JDK versions on Linux and macOS:\n\nAfter installing SDKMAN!, list the available Java 25 distributions:\n\n```\nsdk list java\n```\n\nInstall the Java 25 distribution you prefer, then verify the active version:\n\n```\njava --version\n```\n\nThe project includes the Gradle wrapper, so you do not need to install Gradle separately.\n\nThe Agent Development Kit (ADK) is Google's code-first framework for building and deploying AI agents. It provides the pieces needed to configure models, write agent instructions, connect tools, manage sessions, and run agents locally.\n\nGoogle provides the Kotlin quickstart and API documentation here:\n\nThe complete Kotlin ADK source is also available on GitHub:\n\nAgent Development Kit (ADK) is designed for developers seeking fine-grained control and flexibility when building advanced AI agents that are tightly integrated with services in Google Cloud. It allows you to define agent behavior, orchestration, and tool use directly in code, enabling robust debugging, versioning, and deployment anywhere – from your laptop to the cloud.\n\n**Rich Tool Ecosystem**: Utilize pre-built tools, custom functions, OpenAPI\nspecs, or integrate existing tools to give agents diverse capabilities, all\nfor tight integration with the Google ecosystem.\n\n**Code-First Development**: Define agent logic, tools, and orchestration\ndirectly in Kotlin for ultimate flexibility, testability, and versioning.\n\n**Modular Multi-Agent Systems**: Design scalable applications by composing\nmultiple specialized…\n\nThe Kotlin SDK is published as `com.google.adk:google-adk-kotlin-core`\n\n. This tutorial uses **Kotlin ADK 0.6.0**.\n\nYou need a Gemini Developer API key to run the interactive agent. Create one in Google AI Studio:\n\nThe MCP server and tool-discovery smoke test do not need an API key.\n\nClone the sample repository and run the initialization script. It builds the project and creates a local `.env`\n\nfile from the included template:\n\n```\ngit clone https://github.com/xbill9/adk-hello-world-kotlin\ncd adk-hello-world-kotlin\nsource init.sh\n```\n\nOutput:\n\n```\nCreated .env from .env.example. Add your credentials before running the agent.\nSetup complete. Start ./server.sh, then run ./run.sh in another terminal.\n```\n\nEdit `.env`\n\nand set your API key:\n\n```\nGOOGLE_API_KEY=your-api-key\n```\n\nLoad it into the current shell:\n\n```\nsource set_env.sh\n```\n\nNote:Never commit`.env`\n\n. It is already listed in`.gitignore`\n\n.\n\nThe sample has two Gradle modules:\n\n`agent`\n\ncontains the Kotlin ADK agent and interactive command-line runner.`server`\n\ncontains a Ktor MCP server that exposes the `greet`\n\ntool.The core agent is defined in `GreetingAgent.kt`\n\n. It configures Gemini, gives the agent its instruction, and connects an MCP toolset:\n\n```\nreturn LlmAgent(\n    name = \"kotlin_greeting_agent\",\n    description = \"A Kotlin ADK agent that greets people through an MCP tool.\",\n    model =\n        Gemini(\n            name = modelName,\n            apiKey = apiKey,\n        ),\n    instruction =\n        Instruction(\n            \"\"\"\n            You are a concise greeting assistant.\n            When the user asks you to greet someone, always call the greet tool with that\n            person's name. Return the greeting produced by the tool.\n            \"\"\".trimIndent(),\n        ),\n    toolsets = listOf(mcpToolset),\n)\n```\n\n`LlmAgent`\n\nbrings together the model, instructions, and available tools. The model defaults to `gemini-3.1-flash-lite`\n\n, but you can select another model with the `GEMINI_MODEL`\n\nenvironment variable.\n\nUnlike the TypeScript weather sample, this project keeps the tool in a separate process. The agent discovers and invokes it through the [Model Context Protocol](https://modelcontextprotocol.io/).\n\n`GreetingAgent.kt`\n\ncreates an `McpToolset`\n\nconnected to the local server:\n\n```\nval mcpToolset =\n    McpToolset.McpToolsetConfig(\n        sseConnectionParams =\n            McpConnectionParameters.Sse(\n                url = mcpServerUrl,\n                sseEndpoint = \"sse\",\n            ),\n        toolFilter = listOf(\"greet\"),\n    ).toToolset()\n```\n\nThe connection is lazy. When the agent needs its tools, ADK opens an MCP session, requests the tool list, and makes the `greet`\n\nschema available to Gemini. The tool filter limits this agent to that single tool.\n\nThe server registers the tool in `Tools.kt`\n\n:\n\n```\nserver.addTool(\n    name = Config.Tools.GREET,\n    description = \"Get a greeting from a local HTTP server.\",\n    inputSchema =\n        ToolSchema(\n            properties =\n                buildJsonObject {\n                    put(\n                        Config.Tools.GREET_PARAM,\n                        buildJsonObject {\n                            put(\"type\", \"string\")\n                            put(\"description\", \"The name to greet\")\n                        },\n                    )\n                },\n            required = listOf(Config.Tools.GREET_PARAM),\n        ),\n) { request ->\n    // Read the name and return: Hello, <name>!\n}\n```\n\nThe agent and server communicate over HTTP using Server-Sent Events (SSE). By default, the server listens at `http://localhost:8080`\n\n, with `/sse`\n\nfor the stream and `/messages`\n\nfor client messages.\n\nA single command builds both modules, runs the unit tests, and checks Kotlin formatting:\n\n```\nmake check\n```\n\nYou can call the Gradle tasks directly:\n\n```\n./gradlew build ktlintCheck test\n```\n\nThe tests check that the ADK agent contains its MCP toolset and that the greeting logic returns the expected text. Because the greeting formatter is a plain Kotlin function, it can be tested without calling Gemini:\n\n```\n@Test\nfun testFormatGreeting() {\n    val result = Tools.formatGreeting(\"Kotlin Developer\")\n    assertEquals(\"Hello, Kotlin Developer!\", result)\n}\n```\n\nRun `make format`\n\nif `ktlintCheck`\n\nreports a style issue.\n\nThe tool server and agent run as separate applications. Start the MCP server in one terminal:\n\n```\n./server.sh\n```\n\nIn a second terminal, load the environment and start the agent:\n\n```\nsource set_env.sh\n./run.sh\n```\n\nThe Gradle commands provide the same entry points:\n\n```\n./gradlew :server:run\n./gradlew :agent:run\n```\n\nAsk the agent to greet someone:\n\n```\nGreet Kotlin Developer\n```\n\nGemini selects the discovered `greet`\n\ntool and supplies:\n\n```\n{\"param\":\"Kotlin Developer\"}\n```\n\nThe MCP server returns:\n\n```\nHello, Kotlin Developer!\n```\n\nType `exit`\n\nto close the agent.\n\nYou can verify the MCP connection independently of the model. With the server running, use the Kotlin ADK smoke test:\n\n```\n./gradlew :agent:smokeMcp\n```\n\nThis connects through `McpToolset`\n\nand confirms that the agent can discover `greet`\n\n. It does not require `GOOGLE_API_KEY`\n\n.\n\nThe repository also includes a direct Python JSON-RPC client:\n\n```\npython3 test_mcp.py\n```\n\nIt initializes an MCP session, lists the available tools, calls `greet`\n\nwith `Galaxy`\n\n, and verifies the response `Hello, Galaxy!`\n\n.\n\nThis project deploys the Ktor MCP server as a container. The ADK agent remains a client and connects to the deployed service through `MCP_SERVER_URL`\n\n.\n\nSet your Google Cloud project, then run the deployment script:\n\n```\ngcloud auth login\ngcloud config set project YOUR_PROJECT_ID\n./cloudrun.sh\n```\n\nThe script submits `cloudbuild.yaml`\n\n, which builds the Docker image, pushes it to Container Registry, and deploys the service to Cloud Run.\n\nThe sample stores active SSE sessions in memory, so the supplied Cloud Run configuration limits the service to one instance. It also allows unauthenticated access for demonstration purposes. Add authentication, authorization, stricter CORS rules, and shared session storage before using this design in production.\n\nAfter deployment, retrieve the service URL:\n\n```\ngcloud run services describe adk-hello-world-kotlin \\\n  --region us-central1 --format 'value(status.url)'\n```\n\nPoint the local agent at that URL:\n\n```\nexport MCP_SERVER_URL=\"https://your-service-url\"\n./run.sh\n```\n\nThe Kotlin Agent Development Kit brings agent development to the JVM with familiar Kotlin and Gradle tooling:\n\n`LlmAgent`\n\n, Gemini, and instructions in Kotlin.", "url": "https://wpnews.pro/news/building-ai-agents-with-the-kotlin-agent-development-kit-adk", "canonical_source": "https://dev.to/gde/building-ai-agents-with-the-kotlin-agent-development-kit-adk-2gpa", "published_at": "2026-07-28 18:25:43+00:00", "updated_at": "2026-07-28 18:35:40.061069+00:00", "lang": "en", "topics": ["artificial-intelligence", "developer-tools", "ai-agents"], "entities": ["Google", "JetBrains", "Gemini", "Kotlin Agent Development Kit", "Model Context Protocol", "Ktor", "Gradle", "SDKMAN!"], "alternates": {"html": "https://wpnews.pro/news/building-ai-agents-with-the-kotlin-agent-development-kit-adk", "markdown": "https://wpnews.pro/news/building-ai-agents-with-the-kotlin-agent-development-kit-adk.md", "text": "https://wpnews.pro/news/building-ai-agents-with-the-kotlin-agent-development-kit-adk.txt", "jsonld": "https://wpnews.pro/news/building-ai-agents-with-the-kotlin-agent-development-kit-adk.jsonld"}}