import { z } from "zod";
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
export function serveExamples(category: string) {
const examples: Record<string, string> = {
admin: `// Admin SDK Example
import { init, id } from '@instantdb/admin';
const db = init({
appId: process.env.INSTANT_APP_ID,
adminToken: process.env.INSTANT_APP_ADMIN_TOKEN,
});
// Query data as an admin
const data = await db.query({ goals: {}, todos: {} });
console.error(data);`,
cli: `// Instant CLI Example
// Initialize an Instant app
npx instant-cli@latest init
// Push schema updates to production
npx instant-cli@latest push schema
// Pull latest schema and permissions
npx instant-cli@latest pull`,
modeling: `// Modeling Data Example
import { i } from '@instantdb/core';
const schema = i.schema({
entities: {
posts: i.entity({
title: i.string(),
body: i.string(),
createdAt: i.date(),
}),
},
});
export default schema;`,
patterns: `// Patterns Example
// Restrict creating new attributes in permissions
const perms = {
attrs: { allow: { $default: "false" } },
};
export default perms;`,
reading: `// Reading Data Examples
// Example 1: Fetch all entities from 'goals'
const queryAll = { goals: {} };
const { isLoading: loadingAll, error: errorAll, data: dataAll } = db.useQuery(queryAll);
console.log("All Goals:", dataAll);
// Example 2: Fetch with a filter (where id equals 'goal1')
const queryFilter = { goals: { $: { where: { id: 'goal1' } } } };
const { isLoading: loadingFilter, error: errorFilter, data: dataFilter } = db.useQuery(queryFilter);
console.log("Filtered Goal:", dataFilter);
// Example 3: Fetch nested associations with filtering
const queryAssoc = {
goals: {
$: { where: { title: { $like: '%fit%' } } },
todos: {}
}
};
const { isLoading: loadingAssoc, error: errorAssoc, data: dataAssoc } = db.useQuery(queryAssoc);
console.log("Goals with Todos (filtered):", dataAssoc);`,
linking: `// Linking Example: Schema & Core Usage
// Schema with linking
import { i } from '@instantdb/core';
const schema = i.schema({
entities: {
authors: i.entity({
name: i.string(),
}),
posts: i.entity({
title: i.string(),
content: i.string(),
}),
},
links: {
authorPosts: {
forward: { on: 'posts', has: 'one', label: 'author' },
reverse: { on: 'authors', has: 'many', label: 'posts' },
},
},
});
export default schema;
// Core write and read using linking
import { init } from '@instantdb/admin';
import schema from './path/to/schema';
const db = init({
appId: process.env.INSTANT_APP_ID,
adminToken: process.env.INSTANT_APP_ADMIN_TOKEN,
schema,
});
async function writeAndReadLinkExample() {
// Write: Create an author and a post linked to that author
const authorResult = await db.transact([
db.tx.authors.create({ name: 'Alice' }),
]);
const authorId = authorResult['tx-id'];
const postResult = await db.transact([
db.tx.posts.create({ title: 'Linking Post', content: 'This post is linked to an author' }),
]);
const postId = postResult['tx-id'];
// Link post to author
await db.transact([
db.tx.posts[postId].update({ author: authorId }),
]);
// Read: Query the author with linked posts
const data = await db.query({
authors: {
$: { where: { id: authorId } },
posts: {},
},
});
console.log('Linked Data:', data);
}
writeAndReadLinkExample();`,
};
if (!category || !examples[category]) {
return { content: [{ type: "text", text: Object.keys(examples).join(", ") }] };
}
return { content: [{ type: "text", text: examples[category] }] };
}