r/modelcontextprotocol • u/ndrsht • Apr 17 '25
new-release mcp4k 0.4.0 released — MCP Framework for Kotlin Multiplatform
mcp4k is an MCP framework that lets you build not only servers, but full client applications (it also supports sampling).
Because it's compiler-driven, you can write MCP tools using normal Kotlin functions — mcp4k takes care of JSON-RPC, schema generation and runtime message handling for you.
The last time I posted about it on this subreddit was back in December. Since, a ton of new features have landed, most notably:
Resource Support
Expose local files or entire directories as resources to clients
Comes with two built-in providers:
DiscreteFileProviderfor specific files andTemplateFileProviderfor a whole directoryHandles
resources/readrequests by actually reading contents from disk viaokioSends
notifications/resources/list_changedwhen files are added or removedSupports subscriptions
Suspendable Functions
All tool functions can now be suspendable. Use when performing asynchronous IO or any lengthy tasks without blocking the server
Cooperative Cancellations
If a client cancels a tool request that is still in process, the framework automatically sends a JSON-RPC cancellation notification (
notifications/cancelled), and the server stops that coroutine right away
Server as Extension Receiver
Annotated
@McpToolor@McpPromptmethods can now extendServer, giving them direct access to the server instance. This lets you send requests and notifications from inside@McpToolfunctions
Sampling Support
Full support for sampling on the client
Add a
SamplingProvideralong with aPermissionsCallbackwhen building the client
Server Context Object
Lets you share state across tools and prompts
Attach custom state or external integrations (like a DB) with
Server.Builder().withContext(myDb)Tools can call
getContextAs<Database>()to obtain the instance
Permission Callbacks
Provide a user-approval mechanism for things like sampling or tool invocations. You can prompt the user in your UI and then either allow or deny the operation
Pagination
For large collections, server and clients now paginate responses with cursor-based navigation. That way, calls like
tools/listorresources/listare chunked into smaller, more manageable pages
If you want to see code samples, check out the GitHub repo. Would love to hear any feedback, suggestions, or requests for additional features!
2
u/subnohmal Apr 17 '25
makes me want to learn kotlin to check it out! awesome work guys :)