MCP-Android

nosaywanan/MCP-Android

3.2

If you are the rightful owner of MCP-Android and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.

A robust and extensible Model Context Protocol (MCP) server implementation for Android platforms.

Tools
1
Resources
0
Prompts
0

MCP Server for Android

A robust and extensible Model Context Protocol (MCP) server implementation for Android platforms. This library provides a foundation for building MCP-compliant servers with dynamic tool management, notification broadcasting, and seamless Android integration.

Features

  • Transport Agnostic: Supports multiple communication protocols including SSE and stdio
  • Dynamic Capability Management: Runtime addition/removal of tools, resources, and prompts
  • Notification Broadcasting: Efficiently propagates notifications to all connected clients
  • Android Integration: Seamlessly integrates with Android's logging and concurrency frameworks
  • Dependency Injection: Modular architecture with built-in dependency injection support

Installation

Add the dependency to your build.gradle file:

implementation("open.ai:mcp-core:1.0.0")

Quick Start

Basic Server Implementation

class MyMcpServer(): McpServer(name = "AppName", version = "1.0.0"){  
  
    @mcp.Tools(description = "this tools is used to get weather with city")  
    fun getWeather(city:String):String?{  
        notifier.message("this is message from app")  
        return "this is weather"  
    }  
    @mcp.Prompt(description = "this prompt is used to get weather function call prompt")  
    fun getWeatherPrompt():String{  
        return "this is prompt for getWeather"  
    }  
    @mcp.Prompt(description = "this prompt is used to get some resource")  
    fun getResource(name: String):String{  
        return "this is resource for ${name}"  
    }  
}

Starting the Server

val server = MyMcpServer()

// Start with SSE transport on port 8080
server.sse(8080)

// Or start with HTTP transport (when implemented)
// server.http(8080)

// Stop the server when needed
server.stop()

Transport Protocols

The MCP server supports multiple transport protocols:

SSE (Server-Sent Events)

server.sse(8080)  // Starts SSE server on port 8080

Server will be available at: http://0.0.0.0:8080/sse

Stdio (Standard Input/Output)

// Stdio transport coming soon

HTTP (Planned)

// http-streamable transport don't support by offical mcp-sdk

Advanced Usage

Dependency Injection

class AdvanceMcpServer(): McpServer(name = "AppName", version = "1.0.0"), McpInjectHandler{  
    @mcp.Tools(description = "this tools is used to get weather with city")  
    fun getWeather(city:String):String?{  
        notifier.message("this is message from app")  
        return "this is weather"  
    }  
  
    override fun inject(function: KFunction<*>): Boolean {  
        if (function.name == "getWeather"){  
            return false  
        }  
        return true  
    }  
}

Sending Notifications to MCP Client

The framework provides intelligent parameter injection. When the last parameter of a method is of type Notifier, the framework automatically injects a DefaultNotifier instance for application-level usage.

class MyMcpServer() : McpServer(name = "AppName", version = "1.0.0") {  
  
    @mcp.Tools(description = "This tool retrieves weather information for a specified city")  
    fun getWeather(city: String, notifier: Notifier): String? {  
        // Simply add a Notifier parameter to your function
        notifier.message("This is a message from the application")  
        return "Current weather information"  
    }  
}

Best Practices

  1. Graceful Shutdown: Always call stop() when the server is no longer needed
  2. Resource Management: Remove tools/resources when they're no longer required
  3. Error Handling: Implement proper exception handling for server operations
  4. Thread Safety: Use appropriate coroutine dispatchers for concurrent operations
  5. Notification Efficiency: Only send notifications when necessary to reduce overhead

Contributing

We welcome contributions! Please see our for details.

License

This project is licensed under the MIT License - see the file for details.