> ## Documentation Index
> Fetch the complete documentation index at: https://docs.leanmcp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Advanced Examples

> Authentication and Elicitation examples

# Advanced Examples

Advanced LeanMCP examples demonstrating authentication and user input collection.

<CardGroup cols={2}>
  <Card title="OAuth Authentication" icon="lock" href="https://github.com/Leanmcp-Community/sdk-examples/tree/main/auth-examples/oauth-basic">
    Protect tools with JWT authentication
  </Card>

  <Card title="Elicitation Forms" icon="rectangle-list" href="https://github.com/Leanmcp-Community/sdk-examples/tree/main/elicitation-examples/basic-elicitation">
    Collect user input before tool execution
  </Card>
</CardGroup>

***

## OAuth Authentication

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

### Supported Providers

| Provider        | Package         | Best For                 |
| --------------- | --------------- | ------------------------ |
| **Clerk**       | `@leanmcp/auth` | Quick start, modern apps |
| **AWS Cognito** | `@leanmcp/auth` | AWS ecosystem            |
| **Auth0**       | `@leanmcp/auth` | Enterprise apps          |

### Code Example

```typescript theme={null}
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
```

<Card title="View Full Example" icon="github" href="https://github.com/Leanmcp-Community/sdk-examples/tree/main/auth-examples/oauth-basic">
  OAuth Basic Example
</Card>

***

## 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

```typescript theme={null}
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

| Type          | Description           |
| ------------- | --------------------- |
| `text`        | Single line text      |
| `textarea`    | Multi-line text       |
| `email`       | Email with validation |
| `number`      | Numeric input         |
| `boolean`     | Checkbox              |
| `select`      | Dropdown              |
| `multiselect` | Multiple selection    |
| `date`        | Date picker           |

### Multi-Step Forms

```typescript theme={null}
@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 }
      ]
    }
  ]
})
```

<Card title="View Full Example" icon="github" href="https://github.com/Leanmcp-Community/sdk-examples/tree/main/elicitation-examples/basic-elicitation">
  Elicitation Example
</Card>

***

## Run the Examples

```bash theme={null}
# 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

<CardGroup cols={2}>
  <Card title="Basic Examples" icon="code" href="/examples/basic">
    Tools, Resources, Prompts
  </Card>

  <Card title="Core Concepts" icon="book" href="/core-concepts/tools">
    Learn the fundamentals
  </Card>
</CardGroup>
