Connect MCP Server to Android App & Render Results
In this article
This guide walks you through connecting a Model Context Protocol (MCP) server to an Android app and rendering the results it returns. MCP is an open protocol that standardizes how applications provide context to AI models, but it can also be used for any client-server data exchange. By the end, you'll have a working Android client that fetches data from an MCP server and displays it in a RecyclerView.
What You'll Need
- Android Studio (latest stable version)
- An Android device or emulator running API 24 or higher
- Basic knowledge of Kotlin and Android development
- An MCP server that is already running and accessible (e.g., a local server on your machine or a remote URL)
- Internet permission in your Android manifest (if the server is remote)
Step-by-Step
- Set up your Android project – Open Android Studio and create a new project with an empty Activity. Choose Kotlin as the language. Once the project loads, add the necessary dependencies to your
build.gradle(module-level) file:implementation 'com.squareup.okhttp3:okhttp:4.12.0' implementation 'org.json:json:20240303'
Sync the project. - Add Internet permission – In
AndroidManifest.xml, add<uses-permission android:name="android.permission.INTERNET" />before the<application>tag. If you're connecting to a local server (like10.0.2.2for the emulator), you also need to allow cleartext traffic by addingandroid:usesCleartextTraffic="true"to the<application>tag. - Create a data model – Create a Kotlin data class that represents the structure of the data you expect from the MCP server. For example, if your server returns a list of tasks, create:
data class Task(val id: Int, val title: String, val done: Boolean)
This will be used to parse the JSON response. - Set up the UI – In your
activity_main.xml, add aRecyclerViewand aButtonto trigger the fetch. Give them IDs likerecyclerViewandfetchButton. - Create a RecyclerView adapter – Create a new Kotlin class
TaskAdapterthat extendsRecyclerView.Adapter. Implement the required methods (onCreateViewHolder,onBindViewHolder,getItemCount). In the layout for each item (e.g.,item_task.xml), display the task title and a checkbox for the done status. - Write the MCP client – Create a Kotlin class McpClient that uses OkHttp to make a POST request to your MCP server. The MCP protocol typically uses JSON-RPC. A basic request might look like: Use OkHttp to send this and parse the response. You'll need to know the server's URL and the appropriate method to call (e.g., tools/call with a tool name).
- Fetch data in the Activity – In
MainActivity, set up the RecyclerView with the adapter. On the button click, launch a coroutine (or useThread) to call the MCP client, get the JSON response, parse it into a list ofTaskobjects, and update the adapter on the main thread. - Render the results – Once the adapter receives the data, call
notifyDataSetChanged()to refresh the RecyclerView. Ensure your item layout is properly defined to show the data.
Common Mistakes
- Forgetting to add the Internet permission – the app will crash or fail silently.
- Using the wrong server URL – for the Android emulator,
localhostrefers to the emulator itself; use10.0.2.2to reach your host machine. - Not handling JSON parsing errors – always wrap parsing in try-catch and log the error.
- Updating the UI from a background thread – always use
runOnUiThreador coroutines withDispatchers.Main. - Ignoring the MCP protocol structure – the server expects specific JSON-RPC fields; missing
jsonrpcormethodwill cause errors.
Tips
- Use a library like Retrofit instead of raw OkHttp if you prefer a more structured approach.
- Test your MCP server first with a tool like Postman to see the exact response format.
- Log the raw JSON response in your app to debug parsing issues.
- Consider using ViewModel and LiveData to hold the data and survive configuration changes.
- If your server requires authentication, add the appropriate headers to your OkHttp request.
Troubleshooting
- App crashes on launch – Check the logcat for
SecurityExceptionorUnknownHostException. Ensure the Internet permission is present and the server URL is reachable. - Connection refused – If using the emulator, make sure the server is running on your host machine and you're using
10.0.2.2instead oflocalhost. Also, check that the server is listening on the correct port. - JSON parse errors – The server might return an error object instead of the expected data. Inspect the raw response in logcat. Adjust your data model to match the actual JSON structure.
- RecyclerView shows nothing – Verify that the adapter's
getItemCount()returns the correct size. Also, ensure you callnotifyDataSetChanged()after setting the data. - UI freezes – Network calls must not run on the main thread. Use coroutines with
Dispatchers.IOand then switch toDispatchers.Mainto update the UI.
Frequently Asked Questions
What is MCP?
Model Context Protocol (MCP) is an open protocol that standardizes how applications provide context to AI models. It allows clients to discover and call tools exposed by servers.
Can I use MCP with any server?
No, the server must implement the MCP specification. If you control the server, you can add MCP support; otherwise, you need to check if the server exposes MCP endpoints.
Do I need to use JSON-RPC?
Yes, MCP uses JSON-RPC 2.0 as its message format. Your client must send valid JSON-RPC requests and handle responses accordingly.
How do I find the server URL?
If you're running the server locally, it's usually http://localhost:PORT on your machine. For the Android emulator, use http://10.0.2.2:PORT. For a remote server, use its public IP or domain.
Can I use this with a WebSocket server?
Yes, MCP can be transported over WebSocket. You can use OkHttp's WebSocket support instead of regular HTTP calls.
What if the server returns a large amount of data?
Consider implementing pagination or using a lazy loading approach in your RecyclerView to avoid memory issues.