sodcalculator

10 Tech How-To

Build an Android UI Renderer MCP Server with Jetpack Compose

Guide 10-42078 · Updated · Step by step

This guide walks you through building a Model Context Protocol (MCP) server that renders Android UI definitions as Jetpack Compose code. You’ll create a server that accepts structured UI descriptions (like JSON) and returns Compose composable functions, enabling AI assistants or other tools to generate Android interfaces programmatically. Before starting, you should have basic familiarity with Kotlin, Gradle, and the MCP specification.

What You’ll Need

  • JDK 17 or higher
  • IntelliJ IDEA or Android Studio (for Kotlin development)
  • Gradle 8.x (or use the wrapper)
  • Basic understanding of Jetpack Compose syntax
  • MCP SDK for Java/Kotlin (e.g., io.modelcontextprotocol:kotlin-sdk)

Step-by-Step

  1. Create a new Kotlin project – Use Gradle to set up a JVM application. In build.gradle.kts, add the MCP SDK dependency and the Kotlin plugin. Ensure the main class points to your server entry point.
  2. Define the UI model – Create data classes representing UI elements. For example, UiComponent(type, props, children) where type is a string like “Text” or “Button”, and props is a map of attributes (text, color, onClick). This model will be the input to your renderer.
  3. Implement the renderer function – Write a function that takes a UiComponent and returns a String of Compose code. Use a recursive approach: for each component, generate the composable call and its parameters, then recurse into children. For example, a Text component becomes Text(text = "...", color = Color(...)).
  4. Map props to Compose parameters – Create a mapping from your generic prop names to Compose parameters. Handle common types: strings for text, integers for sizes, boolean for enabled, and color strings like “#FF0000” to Color(0xFFFF0000). Use a when-expression to convert each prop.
  5. Handle layout components – For containers like Column, Row, Box, generate the composable with a trailing lambda containing the children’s code. Ensure indentation is correct for readability.
  6. Set up the MCP server – Use the MCP SDK to create a server with a tool named “render_ui”. The tool’s input schema should accept a JSON string representing the UI tree. In the tool handler, parse the JSON into your UiComponent model, call the renderer, and return the generated code as the tool result.
  7. Register the tool and start the server – In your main function, build the server with the tool definition, then start it with an appropriate transport (e.g., stdio for local use). Log a message when the server is ready.
  8. Test with an MCP client – Use an MCP client (like Claude Desktop or a custom script) to call the tool with a sample UI definition. Verify the generated Compose code is syntactically correct by pasting it into an Android project.

Common Mistakes

  • Forgetting to escape quotes – When generating code as a string, ensure all double quotes inside the generated code are escaped properly. Use triple-quoted strings or escape with backslashes.
  • Ignoring indentation – Generated code without proper indentation is hard to read and may confuse users. Track nesting depth and prepend spaces.
  • Not handling unknown component types – If the input contains an unsupported type, the renderer should throw a clear error or return a comment, not crash.
  • Overcomplicating the model – Start with a minimal set of components (Text, Button, Column, Row). Add more later as needed.

Tips

  • Use a JSON schema for the tool input to validate the structure before parsing.
  • Keep the renderer pure (no side effects) to make testing easier.
  • Write unit tests for the renderer with sample UI trees to ensure output correctness.
  • Consider supporting a “preview” mode that returns a screenshot placeholder, but for now focus on code generation.

Troubleshooting

If the server fails to start, check that the MCP SDK version is compatible with your Java version. If the tool returns an error, verify the input JSON matches your model (e.g., required fields). If the generated Compose code doesn’t compile, compare it with a manually written example to spot missing imports or incorrect parameter names. For transport issues, ensure the client and server use the same transport type (stdio vs. SSE).

Frequently Asked Questions

Can I use this server with any MCP client?

Yes, as long as the client supports MCP and can call tools with JSON input. The server is transport-agnostic.

How do I add a new component type?

Extend the UiComponent model if needed, then add a branch in the renderer’s when-expression that maps the type to the appropriate composable.

Can I generate code for other UI frameworks?

Yes, modify the renderer to output XML or SwiftUI code instead. The architecture remains the same.

What if the UI tree is very large?

The recursive renderer should handle it, but consider adding a depth limit to prevent stack overflow. Also, ensure the MCP tool has a reasonable timeout.

Do I need Android Studio to test the generated code?

No, you can create a simple Kotlin file with the generated code and use the Compose compiler plugin in a JVM project, but full preview requires Android Studio.

Is it possible to render the UI visually?

This server only generates code. To see the UI, you need an Android environment. You could extend the server to call a rendering service, but that’s beyond this guide.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →