Skip to content

docs:(start): add start mcp example #4837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ vite.config.ts.timestamp_*
# Handling VSCode settings
/.vscode/
!/examples/react/**/.vscode/settings.json
!/examples/react/**/.vscode/mcp.json

**/llms

Expand Down
4 changes: 4 additions & 0 deletions docs/start/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@
{
"label": "Material UI",
"to": "framework/react/examples/start-material-ui"
},
{
"label": "MCP Server",
"to": "framework/react/examples/start-mcp"
}
]
},
Expand Down
20 changes: 20 additions & 0 deletions examples/react/start-mcp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
node_modules
package-lock.json
yarn.lock

.DS_Store
.cache
.env
.vercel
.output
/build/
/api/
/server/build
/public/build# Sentry Config File
.env.sentry-build-plugin
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/

count.txt
4 changes: 4 additions & 0 deletions examples/react/start-mcp/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/build
**/public
pnpm-lock.yaml
routeTree.gen.ts
8 changes: 8 additions & 0 deletions examples/react/start-mcp/.vscode/mcp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"servers": {
"start-mcp": {
"type": "http",
"url": "http://localhost:3000/mcp"
}
}
}
11 changes: 11 additions & 0 deletions examples/react/start-mcp/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"files.watcherExclude": {
"**/routeTree.gen.ts": true
},
"search.exclude": {
"**/routeTree.gen.ts": true
},
"files.readonlyInclude": {
"**/routeTree.gen.ts": true
}
}
72 changes: 72 additions & 0 deletions examples/react/start-mcp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Welcome to TanStack.com!

This site is built with TanStack Router!

- [TanStack Router Docs](https://tanstack.com/router)

It's deployed automagically with Netlify!

- [Netlify](https://netlify.com/)

## Development

From your terminal:

```sh
pnpm install
pnpm dev
```

This starts your app in development mode, rebuilding assets on file changes.

## Editing and previewing the docs of TanStack projects locally

The documentations for all TanStack projects except for `React Charts` are hosted on [https://tanstack.com](https://tanstack.com), powered by this TanStack Router app.
In production, the markdown doc pages are fetched from the GitHub repos of the projects, but in development they are read from the local file system.

Follow these steps if you want to edit the doc pages of a project (in these steps we'll assume it's [`TanStack/form`](https://github.com/tanstack/form)) and preview them locally :

1. Create a new directory called `tanstack`.

```sh
mkdir tanstack
```

2. Enter the directory and clone this repo and the repo of the project there.

```sh
cd tanstack
git clone [email protected]:TanStack/tanstack.com.git
git clone [email protected]:TanStack/form.git
```

> [!NOTE]
> Your `tanstack` directory should look like this:
>
> ```
> tanstack/
> |
> +-- form/
> |
> +-- tanstack.com/
> ```

> [!WARNING]
> Make sure the name of the directory in your local file system matches the name of the project's repo. For example, `tanstack/form` must be cloned into `form` (this is the default) instead of `some-other-name`, because that way, the doc pages won't be found.

3. Enter the `tanstack/tanstack.com` directory, install the dependencies and run the app in dev mode:

```sh
cd tanstack.com
pnpm i
# The app will run on https://localhost:3000 by default
pnpm dev
```

4. Now you can visit http://localhost:3000/form/latest/docs/overview in the browser and see the changes you make in `tanstack/form/docs`.

> [!NOTE]
> The updated pages need to be manually reloaded in the browser.

> [!WARNING]
> You will need to update the `docs/config.json` file (in the project's repo) if you add a new doc page!
27 changes: 27 additions & 0 deletions examples/react/start-mcp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "tanstack-start-example-mcp",
"private": true,
"sideEffects": false,
"type": "module",
"scripts": {
"dev": "vite dev",
"build": "vite build && tsc --noEmit",
"start": "vite start"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vite start wont work I guess?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied from the start-counter example but I haven't tried before. I did now and... yeah vite start doesn't seem to work.

Should we replace all vite start with node .output/server/index.mjs in the examples?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well it depends on the deployment target. by default, it's using node, so yes it would work. if someone changes to e.g. bun, the start command would need to be different ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it something related to this PR (adding the new mcp example) only or is it a global decision for all start commands in all examples/docs?

I'd say as long as we explain in the docs how to run in all environments should be fine that we show one in the feature-related examples

},
"dependencies": {
"@modelcontextprotocol/sdk": "^1.17.0",
"@tanstack/react-router": "^1.130.8",
"@tanstack/react-router-devtools": "^1.130.8",
"@tanstack/react-start": "^1.130.8",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"zod": "3.25.76"
},
"devDependencies": {
"@types/node": "^22.5.4",
"@types/react": "^19.0.8",
"@types/react-dom": "^19.0.3",
"typescript": "^5.7.2",
"vite": "^6.3.5"
}
}
107 changes: 107 additions & 0 deletions examples/react/start-mcp/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* eslint-disable */

// @ts-nocheck

// noinspection JSUnusedGlobalSymbols

// This file was automatically generated by TanStack Router.
// You should NOT make any changes in this file as it will be overwritten.
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.

import { createServerRootRoute } from '@tanstack/react-start/server'

import { Route as rootRouteImport } from './routes/__root'
import { Route as IndexRouteImport } from './routes/index'
import { ServerRoute as McpServerRouteImport } from './routes/mcp'

const rootServerRouteImport = createServerRootRoute()

const IndexRoute = IndexRouteImport.update({
id: '/',
path: '/',
getParentRoute: () => rootRouteImport,
} as any)
const McpServerRoute = McpServerRouteImport.update({
id: '/mcp',
path: '/mcp',
getParentRoute: () => rootServerRouteImport,
} as any)

export interface FileRoutesByFullPath {
'/': typeof IndexRoute
}
export interface FileRoutesByTo {
'/': typeof IndexRoute
}
export interface FileRoutesById {
__root__: typeof rootRouteImport
'/': typeof IndexRoute
}
export interface FileRouteTypes {
fileRoutesByFullPath: FileRoutesByFullPath
fullPaths: '/'
fileRoutesByTo: FileRoutesByTo
to: '/'
id: '__root__' | '/'
fileRoutesById: FileRoutesById
}
export interface RootRouteChildren {
IndexRoute: typeof IndexRoute
}
export interface FileServerRoutesByFullPath {
'/mcp': typeof McpServerRoute
}
export interface FileServerRoutesByTo {
'/mcp': typeof McpServerRoute
}
export interface FileServerRoutesById {
__root__: typeof rootServerRouteImport
'/mcp': typeof McpServerRoute
}
export interface FileServerRouteTypes {
fileServerRoutesByFullPath: FileServerRoutesByFullPath
fullPaths: '/mcp'
fileServerRoutesByTo: FileServerRoutesByTo
to: '/mcp'
id: '__root__' | '/mcp'
fileServerRoutesById: FileServerRoutesById
}
export interface RootServerRouteChildren {
McpServerRoute: typeof McpServerRoute
}

declare module '@tanstack/react-router' {
interface FileRoutesByPath {
'/': {
id: '/'
path: '/'
fullPath: '/'
preLoaderRoute: typeof IndexRouteImport
parentRoute: typeof rootRouteImport
}
}
}
declare module '@tanstack/react-start/server' {
interface ServerFileRoutesByPath {
'/mcp': {
id: '/mcp'
path: '/mcp'
fullPath: '/mcp'
preLoaderRoute: typeof McpServerRouteImport
parentRoute: typeof rootServerRouteImport
}
}
}

const rootRouteChildren: RootRouteChildren = {
IndexRoute: IndexRoute,
}
export const routeTree = rootRouteImport
._addFileChildren(rootRouteChildren)
._addFileTypes<FileRouteTypes>()
const rootServerRouteChildren: RootServerRouteChildren = {
McpServerRoute: McpServerRoute,
}
export const serverRouteTree = rootServerRouteImport
._addFileChildren(rootServerRouteChildren)
._addFileTypes<FileServerRouteTypes>()
17 changes: 17 additions & 0 deletions examples/react/start-mcp/src/router.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createRouter as createTanStackRouter } from '@tanstack/react-router'
import { routeTree } from './routeTree.gen'

export function createRouter() {
const router = createTanStackRouter({
routeTree,
scrollRestoration: true,
})

return router
}

declare module '@tanstack/react-router' {
interface Register {
router: ReturnType<typeof createRouter>
}
}
36 changes: 36 additions & 0 deletions examples/react/start-mcp/src/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as React from 'react'
import { HeadContent, Scripts, createRootRoute } from '@tanstack/react-router'
import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'

export const Route = createRootRoute({
head: () => ({
meta: [
{
charSet: 'utf-8',
},
{
name: 'viewport',
content: 'width=device-width, initial-scale=1',
},
{
title: 'TanStack Start MCP Starter',
},
],
}),
shellComponent: RootDocument,
})

function RootDocument({ children }: { children: React.ReactNode }) {
return (
<html>
<head>
<HeadContent />
</head>
<body>
{children}
<TanStackRouterDevtools position="bottom-right" />
<Scripts />
</body>
</html>
)
}
44 changes: 44 additions & 0 deletions examples/react/start-mcp/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as fs from 'node:fs'
import { createFileRoute, useRouter } from '@tanstack/react-router'
import { createServerFn } from '@tanstack/react-start'

const filePath = 'count.txt'

async function readCount() {
return parseInt(
await fs.promises.readFile(filePath, 'utf-8').catch(() => '0'),
)
}

export const getCount = createServerFn({ method: 'GET' }).handler(() => {
return readCount()
})

const updateCount = createServerFn({ method: 'POST' })
.validator((addBy: number) => addBy)
.handler(async ({ data }) => {
const count = await readCount()
await fs.promises.writeFile(filePath, `${count + data}`)
})

export const Route = createFileRoute('/')({
component: Home,
loader: async () => await getCount(),
})

function Home() {
const router = useRouter()
const state = Route.useLoaderData()

return (
<button
onClick={() => {
updateCount({ data: 1 }).then(() => {
router.invalidate()
})
}}
>
Add 1 to {state}?
</button>
)
}
Loading