Skip to content

AI Chat now pointing to MCP server #860

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 11 commits into
base: dev
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
4 changes: 2 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@ai-sdk/google": "^1.2.21",
"@ai-sdk/openai": "^1.3.22",
"@ai-sdk/react": "^1.2.12",
"@modelcontextprotocol/sdk": "^1.12.0",
"@modelcontextprotocol/sdk": "^1.17.2",
"@radix-ui/react-collapsible": "^1.1.11",
"@radix-ui/react-popover": "^1.1.14",
"@radix-ui/react-presence": "^1.1.4",
Expand All @@ -33,7 +33,7 @@
"@stackframe/stack-shared": "workspace:^",
"@vercel/mcp-adapter": "^1.0.0",
"@xyflow/react": "^12.6.4",
"ai": "^4.3.16",
"ai": "^4.3.17",
"class-variance-authority": "^0.7.1",
"fumadocs-core": "15.3.3",
"fumadocs-mdx": "11.6.4",
Expand Down
166 changes: 99 additions & 67 deletions docs/src/app/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createGoogleGenerativeAI } from '@ai-sdk/google';
import { deindent } from '@stackframe/stack-shared/dist/utils/strings';
import { streamText } from 'ai';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { experimental_createMCPClient as createMCPClient, streamText } from 'ai';

// Allow streaming responses up to 30 seconds
export const maxDuration = 30;
Expand All @@ -12,86 +12,118 @@ const google = createGoogleGenerativeAI({

// Helper function to get error message
function getErrorMessage(error: unknown): string {
console.log('Error in chat API:', error);
if (error instanceof Error) {
return error.message;
}
return String(error);
}

export async function POST(request: Request) {
const { messages, docsContent } = await request.json();
const { messages } = await request.json();

// Create MCP client for Stack Auth documentation with error handling
let tools = {};
try {
const stackAuthMcp = await createMCPClient({
transport: new StreamableHTTPClientTransport(
new URL('/api/internal/mcp', 'https://mcp.stack-auth.com')
),
});
tools = await stackAuthMcp.tools();
} catch (error) {
console.error('Failed to initialize MCP client or retrieve tools:', error);
return new Response(
JSON.stringify({
error: 'Documentation service temporarily unavailable',
details: 'Our documentation service is currently unreachable. Please try again in a moment, or visit https://docs.stack-auth.com directly for help.',
}),
{
status: 503,
headers: { 'Content-Type': 'application/json' },
}
);
}

// Create a comprehensive system prompt that restricts AI to Stack Auth topics
const systemPrompt = deindent`
You are Stack Auth's AI assistant. You help users with Stack Auth - a complete authentication and user management solution.

Think step by step about what to say. Being wrong is 100x worse than saying you don't know.

CORE RESPONSIBILITIES:
1. Help users implement Stack Auth in their applications
2. Answer questions about authentication, user management, and authorization using Stack Auth
3. Provide guidance on Stack Auth features, configuration, and best practices
4. Help with framework integrations (Next.js, React, etc.) using Stack Auth

WHAT TO CONSIDER STACK AUTH-RELATED:
- Authentication implementation in any framework (Next.js, React, etc.)
- User management, registration, login, logout
- Session management and security
- OAuth providers and social auth
- Database configuration and user data
- API routes and middleware
- Authorization and permissions
- Stack Auth configuration and setup
- Troubleshooting authentication issues

SUPPORT CONTACT INFORMATION:
When users need personalized support, have complex issues, or ask for help beyond what you can provide from the documentation, direct them to:
- **Discord Community**: https://stack-auth.com/discord (best for quick questions and community help)
- **Email Support**: team@stack-auth.com (for technical support and detailed inquiries)

RESPONSE GUIDELINES:
1. **Be helpful and proactive**: If a question seems related to authentication or user management, assume it's about Stack Auth
2. **Ask follow-up questions**: If you need more context to provide a complete answer, ask specific questions like:
- "Are you using Next.js App Router or Pages Router?"
- "What authentication method are you trying to implement?"
- "What specific issue are you encountering?"
3. **Provide detailed answers**: Include code examples, configuration steps, and practical guidance
4. **Be humble about limitations**: If you're uncertain about something, say "I don't know" or "I'm not sure" rather than claiming something is "not possible" or "impossible"
5. **Avoid definitive negative statements**: Instead of saying something can't be done, explain what you're unsure about and suggest alternatives or ask for clarification
6. **Offer support when appropriate**: If a user has a complex issue, needs personalized help, or you can't fully resolve their problem, suggest contacting support via Discord or email
7. **Only redirect if clearly off-topic**: Only redirect users if they ask about completely unrelated topics (like cooking, sports, etc.)

RESPONSE FORMAT:
- Use markdown formatting for better readability
- Include code blocks with proper syntax highlighting
- Use bullet points for lists
- Bold important concepts
- Provide practical examples when possible
- Focus on giving complete, helpful answers
- **DO NOT reference documentation sections or provide links**
- **DO NOT mention checking documentation, guides, or other resources**
- **Provide all necessary information directly in your response**

WHEN UNSURE:
- If you're unsure about a Stack Auth feature, say "As an AI, I don't know" or "As an AI, I'm not certain" clearly
- Avoid saying things are "not possible" or "impossible", instead say that you don't know
- Ask clarifying questions to better understand the user's needs
- Offer to help with related Stack Auth topics that might be useful
- Provide the best information you can based on your knowledge, but acknowledge limitations
- If the issue is complex or requires personalized assistance, direct them to Discord or email support

Remember: You're here to help users succeed with Stack Auth. Be helpful, ask questions when needed, provide comprehensive guidance for authentication and user management, and don't hesitate to direct users to support channels when they need additional help.

DOCUMENTATION CONTEXT:
${docsContent || 'Documentation not available'}
const systemPrompt = `
# Stack Auth AI Assistant System Prompt

You are Stack Auth's AI assistant. You help users with Stack Auth - a complete authentication and user management solution.

**CRITICAL**: Keep responses SHORT and concise. ALWAYS use the available tools to pull relevant documentation for every question. There should almost never be a question where you don't retrieve relevant docs.

Think step by step about what to say. Being wrong is 100x worse than saying you don't know.

## CORE RESPONSIBILITIES:
1. Help users implement Stack Auth in their applications
2. Answer questions about authentication, user management, and authorization using Stack Auth
3. Provide guidance on Stack Auth features, configuration, and best practices
4. Help with framework integrations (Next.js, React, etc.) using Stack Auth

## WHAT TO CONSIDER STACK AUTH-RELATED:
- Authentication implementation in any framework (Next.js, React, etc.)
- User management, registration, login, logout
- Session management and security
- OAuth providers and social auth
- Database configuration and user data
- API routes and middleware
- Authorization and permissions
- Stack Auth configuration and setup
- Troubleshooting authentication issues

## SUPPORT CONTACT INFORMATION:
When users need personalized support, have complex issues, or ask for help beyond what you can provide from the documentation, direct them to:
- **Discord Community**: https://discord.stack-auth.com (best for quick questions and community help)
- **Email Support**: team@stack-auth.com (for technical support and detailed inquiries)

## RESPONSE GUIDELINES:
1. Be concise and direct. Only provide detailed explanations when specifically requested
2. For every question, use the available tools to retrieve the most relevant documentation sections
3. If you're uncertain, say "I don't know" rather than making definitive negative statements
4. For complex issues or personalized help, suggest Discord or email support

## RESPONSE FORMAT:
- Use markdown formatting for better readability
- Include code blocks with proper syntax highlighting
- Use bullet points for lists
- Bold important concepts
- Provide practical examples when possible
- Focus on giving complete, helpful answers
- **When referencing documentation, use links with the base URL: https://docs.stack-auth.com**
- Example: For setup docs, use https://docs.stack-auth.com/docs/next/getting-started/setup

## WHEN UNSURE:
- If you're unsure about a Stack Auth feature, say "As an AI, I don't know" or "As an AI, I'm not certain" clearly
- Avoid saying things are "not possible" or "impossible", instead say that you don't know
- Ask clarifying questions to better understand the user's needs
- Offer to help with related Stack Auth topics that might be useful
- Provide the best information you can based on your knowledge, but acknowledge limitations
- If the issue is complex or requires personalized assistance, direct them to Discord or email support

## KEY STACK AUTH CONCEPTS TO REMEMBER:
- The core philosophy is complete authentication and user management
- All features work together - authentication, user management, teams, permissions
- Built for modern frameworks like Next.js, React, and more
- Supports multiple authentication methods: OAuth, email/password, magic links
- Team and permission management for multi-tenant applications

## MANDATORY BEHAVIOR:
This is not optional - retrieve relevant documentation for every question.
- Be direct and to the point. Only elaborate when users specifically ask for more detail.

Remember: You're here to help users succeed with Stack Auth. Be helpful but concise, ask questions when needed, always pull relevant docs, and don't hesitate to direct users to support channels when they need additional help.
`;

try {
const result = streamText({
model: google('gemini-2.0-flash'),
model: google('gemini-2.5-flash'),
tools: {
...tools,
},
maxSteps: 50,
system: systemPrompt,
messages,
maxTokens: 1500,
temperature: 0.1,
});

Expand Down
Loading