Skip to main content

Advanced Examples

Advanced LeanMCP examples demonstrating authentication and user input collection.

OAuth Authentication

Protect your MCP tools with JWT authentication using @leanmcp/auth.

Supported Providers

ProviderPackageBest For
Clerk@leanmcp/authQuick start, modern apps
AWS Cognito@leanmcp/authAWS ecosystem
Auth0@leanmcp/authEnterprise apps

Code Example

import { Tool } from "@leanmcp/core";
import { AuthProvider, Authenticated } from "@leanmcp/auth";

// Initialize auth provider
const authProvider = new AuthProvider('clerk', {
  frontendApi: process.env.CLERK_FRONTEND_API,
  secretKey: process.env.CLERK_SECRET_KEY
});
await authProvider.init();

export class SecureService {
  // Public - no auth required
  @Tool({ description: "Public endpoint" })
  async getPublicInfo() {
    return { message: "Anyone can access this" };
  }

  // Protected - requires valid JWT
  @Tool({ description: "Get user profile" })
  @Authenticated(authProvider)
  async getProfile() {
    // authUser is automatically injected!
    return {
      userId: authUser.sub,
      email: authUser.email
    };
  }
}

How It Works

1. Client sends token in _meta.authorization
2. @Authenticated validates JWT with provider
3. authUser injected with decoded payload
4. Tool executes with user context

View Full Example

OAuth Basic Example

Elicitation Forms

Collect structured user input before tool execution using @leanmcp/elicitation.

How Elicitation Works

1. Client calls tool (missing required fields)
2. @Elicitation returns form schema
3. Client displays form to user
4. Client calls tool again (complete data)
5. Tool executes normally

Code Example

import { Tool } from "@leanmcp/core";
import { Elicitation } from "@leanmcp/elicitation";

export class ContactService {
  @Tool({ description: "Submit contact form" })
  @Elicitation({
    title: "Contact Us",
    description: "Please fill out the form",
    fields: [
      {
        name: "name",
        label: "Your Name",
        type: "text",
        required: true
      },
      {
        name: "email",
        label: "Email",
        type: "email",
        required: true
      },
      {
        name: "subject",
        label: "Subject",
        type: "select",
        options: [
          { label: "General", value: "general" },
          { label: "Support", value: "support" },
          { label: "Sales", value: "sales" }
        ]
      },
      {
        name: "message",
        label: "Message",
        type: "textarea",
        required: true,
        validation: {
          minLength: 10,
          maxLength: 1000
        }
      }
    ]
  })
  async submitContact(args: {
    name: string;
    email: string;
    subject: string;
    message: string;
  }) {
    return {
      success: true,
      ticketId: `TICKET-${Date.now()}`
    };
  }
}

Field Types

TypeDescription
textSingle line text
textareaMulti-line text
emailEmail with validation
numberNumeric input
booleanCheckbox
selectDropdown
multiselectMultiple selection
dateDate picker

Multi-Step Forms

@Elicitation({
  strategy: "multi-step",
  builder: () => [
    {
      title: "Step 1: Account",
      fields: [
        { name: "email", label: "Email", type: "email", required: true }
      ]
    },
    {
      title: "Step 2: Details",
      condition: (prev) => prev.accountType === "business",
      fields: [
        { name: "company", label: "Company", type: "text", required: true }
      ]
    }
  ]
})

View Full Example

Elicitation Example

Run the Examples

# Clone the repository
git clone https://github.com/Leanmcp-Community/sdk-examples.git
cd sdk-examples

# Run auth example
cd auth-examples/oauth-basic
npm install && npm run dev

# Run elicitation example
cd elicitation-examples/basic-elicitation
npm install && npm run dev

Next Steps