Skip to content

Commit f75c9ee

Browse files
authored
Added AI chat interface to interact with the MCP inside the typescript-mcp template. (#2970)
<!-- CURSOR_SUMMARY --> > [!NOTE] > Adds a Next.js AI chat web app wired to an Express-based MCP server in the TypeScript MCP template, including ClickHouse query and data catalog tools with rich tool output rendering. > > - **templates/typescript-mcp**: > - **New web app `web-app-with-ai-chat/` (Next.js)**: > - Chat UI with resizable panel, theme toggle, and suggested prompts. > - API routes (`/api/chat`, `/api/chat/status`) using AI SDK v5 and Anthropic. > - MCP client integration; renders tool invocations (incl. timing), reasoning, sources, markdown. > - **Service `moosestack-service/`**: > - Express-based MCP server mounted at `/tools` via `WebApp`. > - Tools: `query_clickhouse` (read-only queries) and `get_data_catalog` (tables/materialized views, summary/detailed). > - Exports service + ingest model (`DataEvent` pipeline); config and README added. > - **libs**: > - Minor formatting change in `FixedString` type. > - **Env/Config**: > - Sample `.env.development`, Tailwind/ESLint/TS configs, and package updates for new apps. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit dfdf821. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 961a7f0 commit f75c9ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+5436
-293
lines changed

packages/ts-moose-lib/src/dataModels/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export type ClickHouseFixedStringSize<N extends number> = {
3030
* sha256_hash: string & FixedString<32>; // 32-byte SHA256
3131
* }
3232
*/
33-
export type FixedString<N extends number> = string & ClickHouseFixedStringSize<N>;
33+
export type FixedString<N extends number> = string &
34+
ClickHouseFixedStringSize<N>;
3435

3536
export type ClickHouseByteSize<N extends number> = {
3637
_clickhouse_byte_size?: N;

pnpm-lock.yaml

Lines changed: 2301 additions & 114 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/typescript-mcp/README.md

Lines changed: 33 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1,203 +1,60 @@
11
# TypeScript MCP Template
22

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).
44

5-
## What is Model Context Protocol (MCP)?
5+
## Overview
66

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:
88

9-
- Access real-time data
10-
- Execute operations
11-
- Interact with external services
12-
- Query databases and APIs
9+
### 1. `moosestack-service/`
1310

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:
1512

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
1717

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/`
2319

24-
## Key Components
20+
A Next.js web application with a pre-configured AI chat interface. This application:
2521

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
5826

5927
## Getting Started
6028

61-
### 1. Install Dependencies
62-
63-
```bash
64-
npm install
65-
```
29+
Both applications can be started independently:
6630

67-
### 2. Start Development Server
31+
### Start the MooseStack Service
6832

6933
```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
17737
```
17838

179-
### TypeScript Errors
180-
181-
Make sure all dependencies are installed:
39+
### Start the Next.js Web App
18240

18341
```bash
42+
cd web-app-with-ai-chat
18443
npm install
44+
npm run dev
18545
```
18646

187-
### ClickHouse Connection Issues
188-
189-
Verify Docker containers are running:
190-
191-
```bash
192-
docker ps
193-
```
47+
## How They Work Together
19448

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
19653

197-
## Support
54+
## Next Steps
19855

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
20059

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.

templates/typescript-mcp/package.json renamed to templates/typescript-mcp/moosestack-service/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
"dev": "moose-cli dev"
88
},
99
"dependencies": {
10-
"@514labs/moose-lib": "latest",
10+
"@514labs/moose-lib": "0.6.190",
1111
"@modelcontextprotocol/sdk": "^1.20.2",
1212
"express": "^5.1.0",
13+
"ts": "link:",
1314
"ts-patch": "^3.3.0",
1415
"tsconfig-paths": "^4.2.0",
1516
"typia": "^9.6.1",
1617
"zod": "^3.0.0"
1718
},
1819
"devDependencies": {
19-
"@514labs/moose-cli": "latest",
20+
"@514labs/moose-cli": "0.6.190",
2021
"@types/express": "^5.0.3",
2122
"@types/node": "^20.12.12"
2223
},

0 commit comments

Comments
 (0)