Skip to content

Commit dfb7ff8

Browse files
committed
break params into two for backward compat
1 parent d09207b commit dfb7ff8

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

src/mcp/server/fastmcp/prompts/manager.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,30 @@ def list_prompts(self) -> list[Prompt]:
2626

2727
def add_prompt(
2828
self,
29-
prompt_or_fn: Prompt | AnyFunction,
29+
prompt: Prompt | None = None,
30+
fn: AnyFunction | None = None,
3031
name: str | None = None,
3132
description: str | None = None,
3233
) -> Prompt:
3334
"""Add a prompt to the manager.
3435
3536
Args:
36-
prompt_or_fn: Either a Prompt instance or a function to create a prompt from
37-
name: Optional name for the prompt (only used if prompt_or_fn is a function)
38-
description: Optional description of the prompt
39-
(only used if prompt_or_fn is a function)
37+
prompt: A Prompt instance (required if fn is not provided)
38+
fn: A function to create a prompt from (required if prompt is not provided)
39+
name: Optional name for the prompt (only used if fn is provided)
40+
description: Optional description of the prompt (only used if fn is provided)
4041
"""
41-
if isinstance(prompt_or_fn, Prompt):
42-
prompt = prompt_or_fn
43-
else:
42+
if prompt is None and fn is None:
43+
raise ValueError("Either prompt or fn must be provided")
44+
if prompt is not None and fn is not None:
45+
raise ValueError("Cannot provide both prompt and fn")
46+
47+
if prompt is None:
48+
# Only call from_function if we have a function to convert
4449
prompt = Prompt.from_function(
45-
prompt_or_fn, name=name, description=description
50+
fn, # type: ignore[arg-type]
51+
name=name,
52+
description=description,
4653
)
4754

4855
# Check for duplicates

src/mcp/server/fastmcp/server.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -491,20 +491,29 @@ def decorator(fn: AnyFunction) -> AnyFunction:
491491

492492
def add_prompt(
493493
self,
494-
prompt_or_fn: Prompt | AnyFunction,
494+
prompt: Prompt | None = None,
495+
fn: AnyFunction | None = None,
495496
name: str | None = None,
496497
description: str | None = None,
497498
) -> None:
498499
"""Add a prompt to the server.
499500
500501
Args:
501-
prompt_or_fn: Either a Prompt instance or a function to create a prompt from
502-
name: Optional name for the prompt (only used if prompt_or_fn is a function)
503-
description: Optional description of the prompt
504-
(only used if prompt_or_fn is a function)
502+
prompt: A Prompt instance (required if fn is not provided)
503+
fn: A function to create a prompt from (required if prompt is not provided)
504+
name: Optional name for the prompt (only used if fn is provided)
505+
description: Optional description of the prompt (only used if fn is provided)
505506
"""
507+
if prompt is None and fn is None:
508+
raise ValueError("Either prompt or fn must be provided")
509+
if prompt is not None and fn is not None:
510+
raise ValueError("Cannot provide both prompt and fn")
511+
506512
self._prompt_manager.add_prompt(
507-
prompt_or_fn, name=name, description=description
513+
prompt=prompt,
514+
fn=fn,
515+
name=name,
516+
description=description,
508517
)
509518

510519
def prompt(
@@ -551,7 +560,7 @@ async def analyze_file(path: str) -> list[Message]:
551560
)
552561

553562
def decorator(func: AnyFunction) -> AnyFunction:
554-
self.add_prompt(func, name=name, description=description)
563+
self.add_prompt(fn=func, name=name, description=description)
555564
return func
556565

557566
return decorator

0 commit comments

Comments
 (0)