Jeremiah Lowin commited on
Commit
06a8bd5
·
1 Parent(s): be6f863

Add typing for content

Browse files
.github/workflows/run-static.yml CHANGED
@@ -28,3 +28,9 @@ jobs:
28
  python-version: "3.12"
29
  - name: Run pre-commit
30
  uses: pre-commit/action@v3.0.1
 
 
 
 
 
 
 
28
  python-version: "3.12"
29
  - name: Run pre-commit
30
  uses: pre-commit/action@v3.0.1
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
.pre-commit-config.yaml CHANGED
@@ -18,8 +18,3 @@ repos:
18
  - id: ruff-format
19
  - id: ruff
20
  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
 
18
  - id: ruff-format
19
  - id: ruff
20
  args: [--fix, --exit-non-zero-on-fix]
 
 
 
 
 
src/fastmcp/prompts/base.py CHANGED
@@ -4,38 +4,42 @@ import json
4
  from typing import Any, Callable, Dict, Literal, Optional, Sequence, Union
5
  import inspect
6
 
7
- from pydantic import BaseModel, Field, TypeAdapter, field_validator, validate_call
8
  from mcp.types import TextContent, ImageContent, EmbeddedResource
9
  import pydantic_core
10
 
 
 
11
 
12
  class Message(BaseModel):
13
  """Base class for all prompt messages."""
14
 
15
  role: Literal["user", "assistant"]
16
- content: Union[TextContent, ImageContent, EmbeddedResource]
17
 
18
- def __init__(self, content, **kwargs):
 
 
19
  super().__init__(content=content, **kwargs)
20
 
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
-
27
 
28
  class UserMessage(Message):
29
  """A message from the user."""
30
 
31
  role: Literal["user"] = "user"
32
 
 
 
 
33
 
34
  class AssistantMessage(Message):
35
  """A message from the assistant."""
36
 
37
  role: Literal["assistant"] = "assistant"
38
 
 
 
 
39
 
40
  message_validator = TypeAdapter(Union[UserMessage, AssistantMessage])
41
 
 
4
  from typing import Any, Callable, Dict, Literal, Optional, Sequence, Union
5
  import inspect
6
 
7
+ from pydantic import BaseModel, Field, TypeAdapter, validate_call
8
  from mcp.types import TextContent, ImageContent, EmbeddedResource
9
  import pydantic_core
10
 
11
+ CONTENT_TYPES = TextContent | ImageContent | EmbeddedResource
12
+
13
 
14
  class Message(BaseModel):
15
  """Base class for all prompt messages."""
16
 
17
  role: Literal["user", "assistant"]
18
+ content: CONTENT_TYPES
19
 
20
+ def __init__(self, content: str | CONTENT_TYPES, **kwargs):
21
+ if isinstance(content, str):
22
+ content = TextContent(type="text", text=content)
23
  super().__init__(content=content, **kwargs)
24
 
 
 
 
 
 
 
25
 
26
  class UserMessage(Message):
27
  """A message from the user."""
28
 
29
  role: Literal["user"] = "user"
30
 
31
+ def __init__(self, content: str | CONTENT_TYPES, **kwargs):
32
+ super().__init__(content=content, **kwargs)
33
+
34
 
35
  class AssistantMessage(Message):
36
  """A message from the assistant."""
37
 
38
  role: Literal["assistant"] = "assistant"
39
 
40
+ def __init__(self, content: str | CONTENT_TYPES, **kwargs):
41
+ super().__init__(content=content, **kwargs)
42
+
43
 
44
  message_validator = TypeAdapter(Union[UserMessage, AssistantMessage])
45