Open History to see the last commit
1// do same for listTanStackReactAddOns and listTanStackSolidAddOns
2// remove the react and solid variants
3
4import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
5import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js'
6import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
7import express from 'express'
8import { z } from 'zod'
9
10import {
11 createApp,
12 createDefaultEnvironment,
13 finalizeAddOns,
14 getFrameworkByName,
15 getFrameworks,
16 populateAddOnOptionsDefaults,
17} from '@tanstack/cta-engine'
18
19function createServer({
20 appName,
21 forcedAddOns = [],
22}: {
23 appName?: string
24 forcedAddOns?: Array<string>
25 name?: string
26}) {
27 const server = new McpServer({
28 name: `${appName} Application Builder`,
29 version: '1.0.0',
30 })
31
32 const frameworks = getFrameworks()
33 const frameworkNames = frameworks.map((framework) => framework.name)
34
35 server.tool(
36 'listTanStackAddOns',
37 'List the available add-ons for creating TanStack applications',
38 {
39 framework: z
40 .string()
41 .describe(
42 `The framework to use. Available frameworks: ${frameworkNames.join(', ')}`,
43 ),
44 },
45 ({ framework: frameworkName }) => {
46 const framework = getFrameworkByName(frameworkName)!
47 return {
48 content: [
49 {
50 type: 'text',
51 text: JSON.stringify(
52 framework
53 .getAddOns()
54 .filter((addOn) => addOn.modes.includes('file-router'))
55 .map((addOn) => ({
56 id: addOn.id,
57 name: addOn.name,
58 description: addOn.description,
59 options: addOn.options,
60 dependsOn: addOn.dependsOn,
61 })),
62 ),
63 },
64 ],
65 }
66 },
67 )
68
69 server.tool(
70 'createTanStackApplication',
71 'Create a new TanStack application',
72 {
73 framework: z
74 .string()
75 .describe(
76 `The framework to use. Available frameworks: ${frameworkNames.join(', ')}`,
77 ),
78 projectName: z
79 .string()
80 .describe(
81 'The package.json module name of the application (will also be the directory name)',
82 ),
83 cwd: z.string().describe('The directory to create the application in'),
84 addOns: z.array(z.string()).describe('Array of add-on IDs to install. Use listTanStackAddOns tool to see available add-ons and their configuration options. Example: ["prisma", "shadcn", "tanstack-query"]'),
85 addOnOptions: z.record(z.record(z.any())).optional().describe('Configuration options for add-ons. Format: {"addOnId": {"optionName": "value"}}. Use listTanStackAddOns to see available options for each add-on.'),
86 targetDir: z
87 .string()
88 .describe(
89 'The directory to create the application in. Use the absolute path of the directory you want the application to be created in',
90 ),
91 },
92 async ({
93 framework: frameworkName,
94 projectName,
95 addOns,
96 addOnOptions,
97 cwd,
98 targetDir,
99 }) => {
100 const framework = getFrameworkByName(frameworkName)!
101 try {
102 process.chdir(cwd)
103 try {
104 const chosenAddOns = await finalizeAddOns(
105 framework,
106 'file-router',
107 Array.from(
108 new Set([
109 ...(addOns as unknown as Array<string>),
110 ...forcedAddOns,
111 ]),
112 ),
113 )
114 await createApp(createDefaultEnvironment(), {
115 projectName: projectName.replace(/^\//, './'),
116 targetDir,
117 framework,
118 typescript: true,
119 tailwind: true,
120 packageManager: 'pnpm',
121 mode: 'file-router',
122 chosenAddOns,
123 addOnOptions: addOnOptions || populateAddOnOptionsDefaults(chosenAddOns),
124 git: true,
125 })
126 } catch (error) {
127 console.error(error)
128 return {
129 content: [
130 { type: 'text', text: `Error creating application: ${error}` },
131 ],
132 }
133 }
134 return {
135 content: [{ type: 'text', text: 'Application created successfully' }],
136 }
137 } catch (error) {
138 return {
139 content: [
140 { type: 'text', text: `Error creating application: ${error}` },
141 ],
142 }
143 }
144 },
145 )
146
147 return server
148}
149
150export async function runMCPServer(
151 sse: boolean,
152 {
153 forcedAddOns,
154 appName,
155 name,
156 }: {
157 forcedMode?: string
158 forcedAddOns?: Array<string>
159 appName?: string
160 name?: string
161 },
162) {
163 let transport: SSEServerTransport | null = null
164
165 const server = createServer({ appName, forcedAddOns, name })
166 if (sse) {
167 const app = express()
168
169 app.get('/sse', (req, res) => {
170 transport = new SSEServerTransport('/messages', res)
171 server.connect(transport)
172 })
173
174 app.post('/messages', (req, res) => {
175 if (transport) {
176 transport.handlePostMessage(req, res)
177 }
178 })
179
180 const port = process.env.PORT || 8080
181 app.listen(port, () => {
182 console.log(`Server is running on port http://localhost:${port}/sse`)
183 })
184 } else {
185 const transport = new StdioServerTransport()
186 await server.connect(transport)
187 }
188}