|
| 1 | +# MCP Web Client Example |
| 2 | + |
| 3 | +This is an example Flutter web application that demonstrates how to use the MCP (Model Context Protocol) client in a web environment. The application provides a simple button-based interface for direct interaction with an MCP server using streamable HTTP connections. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Connect to any MCP-compliant server |
| 8 | +- Discover and call server tools |
| 9 | +- List and retrieve prompts |
| 10 | +- View available resources |
| 11 | +- Receive and display notifications from the server |
| 12 | +- Full support for StreamableHttpClientTransport from the MCP Dart library |
| 13 | + |
| 14 | +## Getting Started |
| 15 | + |
| 16 | +### Prerequisites |
| 17 | + |
| 18 | +- Flutter SDK (latest stable version) |
| 19 | +- Dart SDK (latest stable version) |
| 20 | +- An MCP server to connect to (use the included test server) |
| 21 | + |
| 22 | +### Running the Example |
| 23 | + |
| 24 | +1. First, start an MCP server: |
| 25 | + |
| 26 | +```shell |
| 27 | +cd /path/to/mcp_dart/example/streamable_https |
| 28 | +dart server_streamable_https.dart |
| 29 | +``` |
| 30 | + |
| 31 | +This will start an MCP server on `http://localhost:3000/mcp`. |
| 32 | + |
| 33 | +2. In a separate terminal, run the Flutter web app: |
| 34 | + |
| 35 | +```shell |
| 36 | +cd /path/to/mcp_dart/example/web_client |
| 37 | +flutter run -d chrome |
| 38 | +``` |
| 39 | + |
| 40 | +This will launch the application in Chrome. You can also use other browsers by specifying a different device. |
| 41 | + |
| 42 | +3. In the application, click the "Connect" button to establish a connection to the server. |
| 43 | + |
| 44 | +4. Once connected, you can use various buttons to interact with the server: |
| 45 | + - List Tools: See available tools on the server |
| 46 | + - Call Tool: Execute a tool with arguments |
| 47 | + - List Prompts: View available prompts |
| 48 | + - List Resources: See available resources |
| 49 | + - Start Notifications: Begin receiving server notifications |
| 50 | + |
| 51 | +## Project Structure |
| 52 | + |
| 53 | +- `lib/main.dart` - Entry point of the application |
| 54 | +- `lib/services/streamable_mcp_service.dart` - Service for communicating with MCP servers |
| 55 | +- `lib/screens/mcp_client_screen.dart` - Main UI interface with button controls |
| 56 | +- `lib/screens/settings_screen.dart` - Settings screen for connection management |
| 57 | +- `lib/widgets/chat_widgets.dart` - UI components for the chat interface |
| 58 | +- `test_server.dart` - Simple MCP server for testing |
| 59 | + |
| 60 | +## Understanding MCP Communication |
| 61 | + |
| 62 | +The application demonstrates key aspects of MCP client implementation: |
| 63 | + |
| 64 | +1. **Connection**: The client establishes a connection to the MCP server and retrieves capabilities. |
| 65 | +2. **Tool Calling**: The client calls tools on the server with parameters. |
| 66 | +3. **Notifications**: The client receives real-time notifications from server tools. |
| 67 | +4. **Streaming Responses**: For a more interactive experience, the server can stream partial responses as they are generated. |
| 68 | + |
| 69 | +The `McpClientService` class handles all communication with the server and exposes streams that the UI can listen to for updates. |
| 70 | + |
| 71 | +## Customization |
| 72 | + |
| 73 | +You can modify this example to connect to different MCP servers or implement additional features: |
| 74 | + |
| 75 | +- Change the server URL in the settings |
| 76 | +- Add support for more complex tool parameters |
| 77 | +- Implement authentication for secure MCP servers |
| 78 | +- Add file upload/download capabilities |
| 79 | +- Customize the UI for your specific use case |
| 80 | + |
| 81 | +## Web Compatibility |
| 82 | + |
| 83 | +This example demonstrates the web-compatible implementation of MCP using streamable HTTP connections. The `mcp_dart` library provides platform-specific implementations that work seamlessly in web environments. |
0 commit comments