-
Notifications
You must be signed in to change notification settings - Fork 4.6k
mcp : add initial MCP server example (wip) #3321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
+1,205
−0
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
examples/mcp/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
set(TARGET whisper-mcp-server) | ||
add_executable(${TARGET} mcp-server.cpp stdio-transport.cpp mcp-handler.cpp) | ||
target_include_directories(${TARGET} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
||
set(DEMO_TARGET mcp-demo) | ||
add_executable(${DEMO_TARGET} mcp-demo.cpp stdio-client.cpp) | ||
|
||
include(DefaultTargetOptions) | ||
|
||
target_link_libraries(${TARGET} PRIVATE common json_cpp whisper ${CMAKE_THREAD_LIBS_INIT}) | ||
|
||
# mcp_client only needs json_cpp and threading not whisper. | ||
target_link_libraries(${DEMO_TARGET} PRIVATE json_cpp ${CMAKE_THREAD_LIBS_INIT}) | ||
|
||
install(TARGETS ${TARGET} ${DEMO_TARGET} RUNTIME) |
59 changes: 59 additions & 0 deletions
examples/mcp/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# whisper.cpp/examples/mcp | ||
This directory contains an example of using the Model Context Protocol (MCP) with `whisper.cpp`. The transport | ||
used in this example is the simple input/output (stdin/stdout) transport. When using the input/output transport, | ||
the client is responsible for starting the server as a child process and the communication is done by reading | ||
and writing data to stardard in/out. | ||
|
||
## Usage | ||
The stdio client demo can be run using the following command: | ||
``` | ||
./build/bin/mcp-demo | ||
``` | ||
This will initalize the server using the [initialization] lifecycle phase. | ||
Following that the client will send a request for the list of tools ([tools/list]) that the server supports. | ||
It will then send a request to transcribe an audio file. | ||
|
||
|
||
### Claude.ai Desktop integration | ||
The Whisper.cpp MCP server can be integrated with the Claude.ai Desktop application. | ||
|
||
This requires adding a MCP server configuration to the Claude.ai Desktop: | ||
```console | ||
$ cat ~/Library/Application\ Support/Claude/claude_desktop_config.json | ||
{ | ||
"mcpServers": { | ||
"whisper": { | ||
"command": "/Users/danbev/work/ai/whisper.cpp/build/bin/whisper-mcp-server", | ||
"args": [ | ||
"--model", | ||
"/Users/danbev/work/ai/whisper.cpp/models/ggml-base.en.bin" | ||
] | ||
} | ||
} | ||
} | ||
``` | ||
Update the above paths to match your local system. And then restart the Claude.ai Desktop application. | ||
|
||
After that, clicking on "Connect apps" should show the following: | ||
|
||
 | ||
|
||
And clicking on `[...]` should show the tools available: | ||
|
||
 | ||
|
||
We should then be able to transribe an audio file by using a prompt like this: | ||
```console | ||
Can you transcribe the audio file at /Users/danbev/work/ai/whisper.cpp/samples/jfk.wav? | ||
``` | ||
And this will then prompt for accepting to run the transcription tool: | ||
|
||
 | ||
|
||
And this should result in a successful transcription: | ||
|
||
 | ||
|
||
|
||
[initialization]: https://modelcontextprotocol.io/specification/2025-03-26/basic/lifecycle#initialization | ||
[tools/list]: https://modelcontextprotocol.io/specification/2025-03-26/server/tools#listing-tools |
Binary file added
examples/mcp/images/integration.png
Binary file added
examples/mcp/images/tools.png
Binary file added
examples/mcp/images/transcribe-accept.png
Binary file added
examples/mcp/images/transcribe-screenshot.png
84 changes: 84 additions & 0 deletions
examples/mcp/mcp-demo.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include "stdio-client.hpp" | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
void pretty_print_json(const json & j) { | ||
std::cout << j.dump(2) << std::endl; | ||
} | ||
|
||
int main(int argc, char ** argv) { | ||
std::string server_command = "build/bin/whisper-mcp-server"; | ||
|
||
if (argc > 1) { | ||
server_command = argv[1]; | ||
} | ||
|
||
printf("Starting MCP Demo\n"); | ||
printf("Server command: %s\n", server_command.c_str()); | ||
|
||
try { | ||
mcp::StdioClient client; | ||
|
||
// Start the server | ||
printf("Starting server...\n"); | ||
if (!client.start_server(server_command)) { | ||
fprintf(stderr, "Failed to start server\n"); | ||
return 1; | ||
} | ||
|
||
if (!client.wait_for_server_ready(2000)) { | ||
fprintf(stderr, "Server failed to start within timeout\n"); | ||
return 1; | ||
} | ||
|
||
client.read_server_logs(); | ||
|
||
// Initialize | ||
printf("Initializing...\n"); | ||
json init_response = client.initialize("mcp-demo-client", "1.0.0"); | ||
printf("Initialize response:\n"); | ||
pretty_print_json(init_response); | ||
|
||
if (init_response.contains("error")) { | ||
fprintf(stderr, "Initialization failed!\n"); | ||
return 1; | ||
} | ||
|
||
// Send initialized notification | ||
printf("Sending initialized notification...\n"); | ||
client.send_initialized(); | ||
client.read_server_logs(); | ||
|
||
// List tools | ||
printf("Listing tools...\n"); | ||
json tools_response = client.list_tools(); | ||
printf("Tools list response:\n"); | ||
pretty_print_json(tools_response); | ||
|
||
// Call transcribe tool | ||
printf("Calling transcribe tool...\n"); | ||
json transcribe_args = { | ||
{"file", "samples/jfk.wav"} | ||
}; | ||
|
||
json transcribe_response = client.call_tool("transcribe", transcribe_args); | ||
printf("Transcribe response:\n"); | ||
pretty_print_json(transcribe_response); | ||
|
||
// Call model info tool | ||
printf("Calling model info tool...\n"); | ||
json model_info_response = client.call_tool("model_info", json::object()); | ||
printf("Model info response:\n"); | ||
pretty_print_json(model_info_response); | ||
|
||
// Final logs | ||
printf("Final server logs:\n"); | ||
client.read_server_logs(); | ||
} catch (const std::exception & e) { | ||
fprintf(stderr, "Exception: %s\n", e.what()); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.