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