Skip to content

Commit 5cb154f

Browse files
committed
Add typing for content
1 parent 83eb81f commit 5cb154f

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

Diff for: .github/workflows/run-static.yml

+6
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ jobs:
2828
python-version: "3.12"
2929
- name: Run pre-commit
3030
uses: pre-commit/[email protected]
31+
- name: Install dependencies
32+
run: |
33+
python -m pip install --upgrade pip
34+
pip install ".[tests]"
35+
- name: Run pyright
36+
run: pyright src tests

Diff for: .pre-commit-config.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,3 @@ repos:
1818
- id: ruff-format
1919
- id: ruff
2020
args: [--fix, --exit-non-zero-on-fix]
21-
22-
- repo: https://github.com/RobertCraigie/pyright-python
23-
rev: v1.1.352
24-
hooks:
25-
- id: pyright

Diff for: src/fastmcp/prompts/base.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,42 @@
44
from typing import Any, Callable, Dict, Literal, Optional, Sequence, Union
55
import inspect
66

7-
from pydantic import BaseModel, Field, TypeAdapter, field_validator, validate_call
7+
from pydantic import BaseModel, Field, TypeAdapter, validate_call
88
from mcp.types import TextContent, ImageContent, EmbeddedResource
99
import pydantic_core
1010

11+
CONTENT_TYPES = TextContent | ImageContent | EmbeddedResource
12+
1113

1214
class Message(BaseModel):
1315
"""Base class for all prompt messages."""
1416

1517
role: Literal["user", "assistant"]
16-
content: Union[TextContent, ImageContent, EmbeddedResource]
18+
content: CONTENT_TYPES
1719

18-
def __init__(self, content, **kwargs):
20+
def __init__(self, content: str | CONTENT_TYPES, **kwargs):
21+
if isinstance(content, str):
22+
content = TextContent(type="text", text=content)
1923
super().__init__(content=content, **kwargs)
2024

21-
@field_validator("content", mode="before")
22-
def validate_content(cls, v):
23-
if isinstance(v, str):
24-
return TextContent(type="text", text=v)
25-
return v
26-
2725

2826
class UserMessage(Message):
2927
"""A message from the user."""
3028

3129
role: Literal["user"] = "user"
3230

31+
def __init__(self, content: str | CONTENT_TYPES, **kwargs):
32+
super().__init__(content=content, **kwargs)
33+
3334

3435
class AssistantMessage(Message):
3536
"""A message from the assistant."""
3637

3738
role: Literal["assistant"] = "assistant"
3839

40+
def __init__(self, content: str | CONTENT_TYPES, **kwargs):
41+
super().__init__(content=content, **kwargs)
42+
3943

4044
message_validator = TypeAdapter(Union[UserMessage, AssistantMessage])
4145

0 commit comments

Comments
 (0)