# `@lazarv/react-server/mcp`

Model Context Protocol primitives. Build typed tools, resources, and prompts, then expose them through an MCP server route handler.

## Functions

### `createPrompt`

```ts
function createPrompt<S extends InputSchemaShape, R>(def: PromptDefinition<S, R>): (input: {
    [K in keyof S]: z.infer<S[K]>;
}) => Promise<R>;
```

Creates a prompt that can be used in the Model Context Protocol (MCP).
The prompt can be used to gather input from the user and return a result based on the provided input schema.

**Parameters**

- `def` — The definition of the prompt, including its ID, title, optional description, input schema, and handler function.

**Returns** — A function that takes input matching the schema and returns a promise that resolves to the result of the prompt's action.

### `createResource`

```ts
function createResource<T extends string & {}, R>(def: ResourceDefinition<T, R>): (input: ParamsFromTemplate<T>) => Promise<R>;
```

Creates a resource that can be used in the Model Context Protocol (MCP).
The resource can be accessed using a template string, allowing for dynamic parameters.

**Parameters**

- `def` — The definition of the resource, including its ID, template, list function, optional completion functions for parameters, title, description, and handler function.

**Returns** — A function that takes input matching the template parameters and returns a promise that resolves to the result of the resource's action.

### `createServer`

```ts
function createServer<T, R, P>({ tools, resources, prompts, }: {
    tools?: T;
    resources?: R;
    prompts?: P;
}): (context: HttpContext) => Promise<Response>;
```

Creates a Model Context Protocol (MCP) server that can handle requests and responses.
The server can be configured with tools, resources, and prompts.

**Parameters**

- `tools` — An optional object containing tools defined using `createTool`.
- `resources` — An optional object containing resources defined using `createResource`.
- `prompts` — An optional object containing prompts defined using `createPrompt`.

**Returns** — A function that takes an `HttpContext` and returns a promise that resolves to a `Response`. A `@lazarv/react-server` server middleware can use this function to handle requests.

### `createTool`

```ts
function createTool<S extends InputSchemaShape, R>(def: ToolDefinition<S, R>): (input: {
    [K in keyof S]: z.infer<S[K]>;
}) => Promise<R>;
```

Creates a tool that can be used in the Model Context Protocol (MCP).
The tool can be used to perform actions or retrieve data based on the provided input schema.

**Parameters**

- `def` — The definition of the tool, including its ID, title, description, input schema, and handler function.

**Returns** — A function that takes input matching the schema and returns a promise that resolves to the result of the tool's action.

### `isMCPCall`

```ts
function isMCPCall(): boolean;
```

Checks if the current call is an MCP call.
This function can be used to determine if the current request is being handled by an MCP server.

**Returns** — `true` if the current call is an MCP call, otherwise `false`.

### `useMCPServer`

```ts
function useMCPServer(): McpServer | undefined;
```

Retrieves the current Model Context Protocol (MCP) server from the request context.
This function is typically used within a server-side component to access the MCP server instance.

**Returns** — The current MCP server instance, or `undefined` if not available.
