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