|
1 | 1 | # TypeScript MCP Template |
2 | 2 |
|
3 | | -This template demonstrates how to integrate the **Model Context Protocol (MCP)** with MooseStack using Express and the `WebApp` class. It showcases MooseStack's "bring your own API framework" capability. |
| 3 | +This template provides a complete example of building AI-powered applications with MooseStack and the Model Context Protocol (MCP). |
4 | 4 |
|
5 | | -## What is Model Context Protocol (MCP)? |
| 5 | +## Overview |
6 | 6 |
|
7 | | -[Model Context Protocol (MCP)](https://modelcontextprotocol.io) is an open protocol that enables AI assistants to securely connect to data sources and tools. It provides a standardized way for LLMs to: |
| 7 | +This template contains two independent applications that work together: |
8 | 8 |
|
9 | | -- Access real-time data |
10 | | -- Execute operations |
11 | | -- Interact with external services |
12 | | -- Query databases and APIs |
| 9 | +### 1. `moosestack-service/` |
13 | 10 |
|
14 | | -## What This Template Demonstrates |
| 11 | +A MooseStack service template that demonstrates how to build a data service with an integrated MCP server. This service: |
15 | 12 |
|
16 | | -This template shows how to: |
| 13 | +- Provides a complete MooseStack data pipeline example |
| 14 | +- Exposes an MCP server that AI agents can connect to |
| 15 | +- Includes data ingestion, processing, and API capabilities |
| 16 | +- Can be run independently for development and testing |
17 | 17 |
|
18 | | -1. **Create custom API endpoints** using Express within MooseStack |
19 | | -2. **Implement an MCP server** using `@modelcontextprotocol/sdk` |
20 | | -3. **Mount custom web apps** at specific paths using the `WebApp` class |
21 | | -4. **Define data models** with ClickHouse table creation |
22 | | -5. **Expose tools** that AI assistants can use via MCP |
| 18 | +### 2. `web-app-with-ai-chat/` |
23 | 19 |
|
24 | | -## Key Components |
| 20 | +A Next.js web application with a pre-configured AI chat interface. This application: |
25 | 21 |
|
26 | | -### 1. Data Model (`app/ingest/models.ts`) |
27 | | - |
28 | | -Defines a minimal `DataEvent` model with ClickHouse table creation. The IngestPipeline creates the ClickHouse table, enables Kafka streaming, and provides a POST endpoint at `/ingest/DataEvent`. |
29 | | - |
30 | | -### 2. MCP Server (`app/apis/mcp.ts`) |
31 | | - |
32 | | -Implements an MCP server using `@modelcontextprotocol/sdk`: |
33 | | - |
34 | | -- Uses **StreamableHTTPServerTransport** with JSON responses (stateless mode) |
35 | | -- Registers a `query_clickhouse` tool for AI assistants |
36 | | -- Mounts at `/tools` to avoid conflict with built-in `/mcp` endpoint |
37 | | -- Accesses ClickHouse via `getMooseUtils()` for query execution |
38 | | -- Fresh server instance created for every request |
39 | | - |
40 | | -**Security Features:** |
41 | | - |
42 | | -- SQL query whitelist: Only SELECT, SHOW, DESCRIBE, EXPLAIN queries permitted |
43 | | -- SQL query blocklist: Prevents INSERT, UPDATE, DELETE, DROP, CREATE, ALTER, TRUNCATE, etc. |
44 | | -- Row limit enforcement: Results automatically capped at 100 rows maximum |
45 | | - |
46 | | -### 3. SQL Security (`app/apis/utils/sql.ts`) |
47 | | - |
48 | | -Provides SQL validation and sanitization utilities: |
49 | | - |
50 | | -- `validateQuery()`: Main validation function combining whitelist and blocklist checks |
51 | | -- `validateQueryWhitelist()`: Ensures query starts with allowed SQL keywords |
52 | | -- `validateQueryBlocklist()`: Blocks dangerous SQL operations |
53 | | -- `applyLimitToQuery()`: Enforces maximum row limits on SELECT queries |
54 | | - |
55 | | -Uses regex-based pattern matching for ClickHouse SQL validation without external parser dependencies. |
56 | | - |
57 | | -**Note:** This is a demonstration implementation. A production-ready MCP server would require more robust query sanitization beyond regex pattern matching, such as a proper SQL parser, parameterized queries, and comprehensive injection prevention mechanisms. |
| 22 | +- Features a modern, responsive layout with an integrated chat panel |
| 23 | +- Includes a fully functional AI chat interface out of the box |
| 24 | +- Is configured to connect to the MooseStack service MCP server |
| 25 | +- Provides a ready-to-use foundation for building AI-powered user experiences |
58 | 26 |
|
59 | 27 | ## Getting Started |
60 | 28 |
|
61 | | -### 1. Install Dependencies |
62 | | - |
63 | | -```bash |
64 | | -npm install |
65 | | -``` |
| 29 | +Both applications can be started independently: |
66 | 30 |
|
67 | | -### 2. Start Development Server |
| 31 | +### Start the MooseStack Service |
68 | 32 |
|
69 | 33 | ```bash |
70 | | -moose dev |
71 | | -``` |
72 | | - |
73 | | -The MCP server will be available at `http://localhost:4000/tools` |
74 | | - |
75 | | -### 3. Test the MCP Server |
76 | | - |
77 | | -The MCP server will be available at `http://localhost:4000/tools`. You can test it by sending JSON-RPC requests to list available tools or execute the `query_clickhouse` tool. |
78 | | - |
79 | | -## MCP Tools Available |
80 | | - |
81 | | -### `query_clickhouse` |
82 | | - |
83 | | -Executes SQL queries against the ClickHouse database with security validation and automatic result limiting. |
84 | | - |
85 | | -**Input Parameters:** |
86 | | - |
87 | | -- `query` (required): SQL query to execute against ClickHouse (must be SELECT, SHOW, DESCRIBE, or EXPLAIN) |
88 | | -- `limit` (optional): Maximum number of rows to return (default: 100, max: 100) |
89 | | - |
90 | | -**Output:** |
91 | | - |
92 | | -- `rows`: Array of row objects containing query results |
93 | | -- `rowCount`: Number of rows returned |
94 | | - |
95 | | -**Security:** |
96 | | - |
97 | | -- Only read-only queries are allowed (SELECT, SHOW, DESCRIBE, EXPLAIN) |
98 | | -- Write operations (INSERT, UPDATE, DELETE) are blocked |
99 | | -- DDL operations (DROP, CREATE, ALTER, TRUNCATE) are blocked |
100 | | -- Results are automatically limited to 100 rows maximum |
101 | | - |
102 | | -## Using with Claude Code |
103 | | - |
104 | | -You can configure Claude Code to connect to your MCP server by adding it to your Claude Code configuration: |
105 | | - |
106 | | -```bash |
107 | | -claude mcp add --transport http clickhouse http://localhost:4000/tools |
108 | | -``` |
109 | | - |
110 | | -Once connected, you can ask Claude Code questions like: |
111 | | - |
112 | | -- "What tables exist in the database?" |
113 | | -- "Show me the latest 10 events from the DataEvent table" |
114 | | -- "How many events are in the DataEvent table?" |
115 | | - |
116 | | -Claude Code will automatically use the `query_clickhouse` tool to execute the appropriate SQL queries. |
117 | | - |
118 | | -## Security Features |
119 | | - |
120 | | -This template implements several security measures for safe database querying: |
121 | | - |
122 | | -### ✅ Implemented |
123 | | - |
124 | | -- **SQL Query Validation**: Whitelist/blocklist validation ensures only safe, read-only queries |
125 | | - - Allowed: SELECT, SHOW, DESCRIBE, EXPLAIN |
126 | | - - Blocked: INSERT, UPDATE, DELETE, DROP, CREATE, ALTER, TRUNCATE, GRANT, REVOKE, EXECUTE, CALL |
127 | | -- **Row Limiting**: Results automatically capped at 100 rows to prevent excessive data transfer |
128 | | -- **Error Handling**: Security errors returned through MCP protocol without exposing internals |
129 | | - |
130 | | -### ⚠️ Production Considerations |
131 | | - |
132 | | -Before deploying to production, consider adding: |
133 | | - |
134 | | -- **Authentication & Authorization**: JWT authentication framework is in place (see TODO in mcp.ts) |
135 | | -- **Rate Limiting**: Protect against abuse and DoS attacks |
136 | | -- **Query Timeouts**: Prevent long-running queries from consuming resources |
137 | | -- **Audit Logging**: Track who executed which queries and when |
138 | | -- **IP Whitelisting**: Restrict access to known clients |
139 | | -- **TLS/HTTPS**: Encrypt data in transit |
140 | | - |
141 | | -The current implementation provides a secure foundation for read-only database access but should be enhanced with additional production-grade features based on your deployment requirements. |
142 | | - |
143 | | -## Testing Data Ingestion |
144 | | - |
145 | | -You can send test events to the DataEvent table via POST requests to `http://localhost:4000/ingest/DataEvent` with JSON payloads containing `eventId`, `timestamp`, `eventType`, and `data` fields. |
146 | | - |
147 | | -## Extending This Template |
148 | | - |
149 | | -### Adding More Tools |
150 | | - |
151 | | -Register additional tools in `app/apis/mcp.ts` using `server.registerTool()`. Each tool needs a name, title, description, input/output schemas (using Zod), and an async handler function. |
152 | | - |
153 | | -### Accessing MooseStack Utilities |
154 | | - |
155 | | -Use `getMooseUtils(req)` in your endpoint handlers to access the ClickHouse client (`client.query.execute()`) and SQL template function (`sql`) for safe query execution. |
156 | | - |
157 | | -### Adding More Data Models |
158 | | - |
159 | | -Create additional data models in `app/ingest/models.ts` by defining interfaces and creating IngestPipeline instances with options for `table`, `stream`, and `ingestApi`. |
160 | | - |
161 | | -## Learn More |
162 | | - |
163 | | -- [MooseStack Documentation](https://docs.moosejs.com) |
164 | | -- [Model Context Protocol](https://modelcontextprotocol.io) |
165 | | -- [MCP SDK (@modelcontextprotocol/sdk)](https://github.com/modelcontextprotocol/typescript-sdk) |
166 | | -- [WebApp Class Reference](https://docs.moosejs.com/building-moose-apps/custom-apis) |
167 | | - |
168 | | -## Troubleshooting |
169 | | - |
170 | | -### Port Already in Use |
171 | | - |
172 | | -If port 4000 is already in use, update `moose.config.toml`: |
173 | | - |
174 | | -```toml |
175 | | -[server] |
176 | | -port = 4001 |
| 34 | +cd moosestack-service |
| 35 | +npm install |
| 36 | +npm run dev |
177 | 37 | ``` |
178 | 38 |
|
179 | | -### TypeScript Errors |
180 | | - |
181 | | -Make sure all dependencies are installed: |
| 39 | +### Start the Next.js Web App |
182 | 40 |
|
183 | 41 | ```bash |
| 42 | +cd web-app-with-ai-chat |
184 | 43 | npm install |
| 44 | +npm run dev |
185 | 45 | ``` |
186 | 46 |
|
187 | | -### ClickHouse Connection Issues |
188 | | - |
189 | | -Verify Docker containers are running: |
190 | | - |
191 | | -```bash |
192 | | -docker ps |
193 | | -``` |
| 47 | +## How They Work Together |
194 | 48 |
|
195 | | -You should see containers for ClickHouse, Redpanda, and Temporal. |
| 49 | +1. The **moosestack-service** runs your data pipeline and exposes an MCP server that provides AI agents with access to your data and tools |
| 50 | +2. The **web-app-with-ai-chat** provides a user interface where users can interact with an AI agent |
| 51 | +3. The AI agent in the web app connects to the MCP server to access your data and capabilities |
| 52 | +4. Users can chat naturally with the AI, which uses the MCP server to answer questions and perform actions on your data |
196 | 53 |
|
197 | | -## Support |
| 54 | +## Next Steps |
198 | 55 |
|
199 | | -For questions or issues: |
| 56 | +- Customize the MooseStack service with your own data models and APIs |
| 57 | +- Extend the AI chat interface with additional features and integrations |
| 58 | +- Configure the MCP connection settings to match your deployment environment |
200 | 59 |
|
201 | | -- [GitHub Issues](https://github.com/514-labs/moose) |
202 | | -- [Discord Community](https://discord.gg/moose) |
203 | | -- [Documentation](https://docs.moosejs.com) |
| 60 | +For more detailed information, see the README files in each subdirectory. |
0 commit comments