# Server

## App factory

## create\_app

```python
create_app(
    output_dir: str = "./received",
    store_as: StoreFormat = "msz",
    auth: AuthProvider | Callable[..., Any] | None = None,
    file_provider: FileProvider | None = None,
    *,
    upload_auth: AuthProvider
    | Callable[..., Any]
    | None = None,
    download_auth: AuthProvider
    | Callable[..., Any]
    | None = None,
) -> FastAPI
```

Create a configured mstransfer FastAPI application.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output_dir` | `str` | Directory where received files are stored. | `'./received'` |
| `store_as` | `StoreFormat` | Output format — "msz" or "mzml". | `'msz'` |
| `auth` | `AuthProvider \| Callable[..., Any] \| None` | Authentication provider, raw FastAPI dependency, or *None* (no auth). Protects both uploads and downloads. Can't be combined with *upload\_auth* or *download\_auth*. | `None` |
| `file_provider` | `FileProvider \| None` | File discovery provider, or *None* to default to scanning *output\_dir* on disk. | `None` |
| `upload_auth` | `AuthProvider \| Callable[..., Any] \| None` | Like *auth*, but only for `POST /v1/upload` and `GET /v1/transfer/{id}/status`. | `None` |
| `download_auth` | `AuthProvider \| Callable[..., Any] \| None` | Like *auth*, but only for `GET /v1/files` and `GET /v1/files/{filename}`. | `None` |

Raises:

| Type | Description |
| --- | --- |
| `TypeError` | If *auth* is combined with *upload\_auth* or *download\_auth*, or any of them has an unsupported type. |

## Router

## make\_router

```python
make_router(
    auth_dep: Callable[..., Any] | None = None,
    state: AppState | None = None,
    *,
    upload_auth_dep: Callable[..., Any] | None = None,
    download_auth_dep: Callable[..., Any] | None = None,
) -> APIRouter
```

Create the API router, optionally protecting routes with auth dependencies.

Routes are split into two groups that can be protected separately:

- upload: `POST /upload` and `GET /transfer/{transfer_id}/status`
- download: `GET /files` and `GET /files/{filename}`

`GET /health` is never protected.

Parameters:

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `auth_dep` | `Callable[..., Any] \| None` | Optional FastAPI dependency applied to both groups. Can't be combined with *upload\_auth\_dep* or *download\_auth\_dep*. | `None` |
| `state` | `AppState \| None` | Optional `AppState` instance. When provided the router uses it directly (via a closure) instead of reading `request.app.state`. This allows the router to be included in a parent app with `app.include_router(...)` without requiring the parent to set up `app.state`. | `None` |
| `upload_auth_dep` | `Callable[..., Any] \| None` | Optional dependency for the upload group only. | `None` |
| `download_auth_dep` | `Callable[..., Any] \| None` | Optional dependency for the download group only. | `None` |

Raises:

| Type | Description |
| --- | --- |
| `TypeError` | If *auth\_dep* is combined with *upload\_auth\_dep* or *download\_auth\_dep*. |

## Authentication

## auth

Extensible authentication for mstransfer.

### AuthContext

Bases: `BaseModel`

Identity returned by an auth provider.

### AuthProvider

Bases: `Protocol`

Contract that auth providers must satisfy.

### NoAuthProvider

Default no-op provider — allows all requests.

### APIKeyAuthProvider

```python
APIKeyAuthProvider(api_key: str)
```

Validates `Authorization: Bearer <key>` or `?api_key=` query param.

### make\_auth\_dependency

```python
make_auth_dependency(
    provider: AuthProvider,
) -> Callable[[Request], Awaitable[AuthContext]]
```

Convert an AuthProvider into a FastAPI dependency callable.

## File providers

## fileprovider

Extensible file discovery for mstransfer.

### FileProvider

Bases: `Protocol`

Contract that file providers must satisfy.

Implementations may return `FileInfo` subclasses from `list_files`; the return type is `Sequence` so that subclass lists are accepted without type errors.

`get_file` returns the file to serve for *filename*, or `None` for a 404. A `Path` is served under its own name. When the file is stored under a different name (e.g. a content-addressed blob with no extension), return a `ServedFile` instead: its `path` is streamed and its `name` is used for `Content-Disposition` and the media type.

### DirectoryFileProvider

```python
DirectoryFileProvider(directory: Path)
```

Default provider — scans a directory for .msz/.mszx/.mzML files.

#### list\_files

```python
list_files() -> Sequence[FileInfo]
```

Return metadata for every .msz / .mszx / .mzML file in the directory.

#### get\_file

```python
get_file(filename: str) -> Path | None
```

Resolve *filename* to a path inside the directory.

Returns *None* if the file does not exist or the name attempts directory traversal.

## State

## state

### AppState

```python
AppState(
    output_dir: Path,
    store_as: StoreFormat,
    files: FileProvider,
)
```

Bases: `State`

Application state object to hold configuration and transfer registry.

### OutputClaims

```python
OutputClaims()
```

Output paths that in-flight uploads are writing.

Lets an upload refuse a destination another transfer already holds, so two uploads never write to the same file. Paths compare case-insensitively, as macOS and Windows filesystems do.

#### claim

```python
claim(path: Path) -> bool
```

Reserve *path*, returning *False* if it is already reserved.

#### release

```python
release(path: Path) -> None
```

Give up a reservation made with `claim`.

### TransferRegistry

```python
TransferRegistry(maxsize: int = 1024)
```

In-memory LRU registry of transfer records.

When the number of records exceeds *maxsize*, the least-recently-used **terminal** (DONE / ERROR) record is evicted. In-progress transfers are never evicted; the registry may temporarily exceed *maxsize* if all records are active.
