|
60 | 60 | from fastmcp.prompts import Prompt |
61 | 61 | from fastmcp.prompts.prompt import FunctionPrompt |
62 | 62 | from fastmcp.prompts.prompt_manager import PromptManager |
63 | | -from fastmcp.resources.resource import Resource |
| 63 | +from fastmcp.resources.resource import FunctionResource, Resource |
64 | 64 | from fastmcp.resources.resource_manager import ResourceManager |
65 | | -from fastmcp.resources.template import ResourceTemplate |
| 65 | +from fastmcp.resources.template import FunctionResourceTemplate, ResourceTemplate |
66 | 66 | from fastmcp.server.auth import AuthProvider |
67 | 67 | from fastmcp.server.http import ( |
68 | 68 | StarletteWithLifespan, |
@@ -400,48 +400,21 @@ async def _docket_lifespan(self) -> AsyncIterator[None]: |
400 | 400 | self._docket = docket |
401 | 401 |
|
402 | 402 | # Register local task-enabled tools/prompts/resources with Docket |
| 403 | + # Only function-based variants support background tasks |
403 | 404 | for tool in self._tool_manager._tools.values(): |
404 | | - if not hasattr(tool, "fn"): |
405 | | - continue |
406 | | - supports_task = ( |
407 | | - tool.task |
408 | | - if tool.task is not None |
409 | | - else self._support_tasks_by_default |
410 | | - ) |
411 | | - if supports_task: |
| 405 | + if isinstance(tool, FunctionTool) and tool.task: |
412 | 406 | docket.register(tool.fn) |
413 | 407 |
|
414 | 408 | for prompt in self._prompt_manager._prompts.values(): |
415 | | - if not hasattr(prompt, "fn"): |
416 | | - continue |
417 | | - supports_task = ( |
418 | | - prompt.task |
419 | | - if prompt.task is not None |
420 | | - else self._support_tasks_by_default |
421 | | - ) |
422 | | - if supports_task: |
| 409 | + if isinstance(prompt, FunctionPrompt) and prompt.task: |
423 | 410 | docket.register(prompt.fn) |
424 | 411 |
|
425 | 412 | for resource in self._resource_manager._resources.values(): |
426 | | - if not hasattr(resource, "fn"): |
427 | | - continue |
428 | | - supports_task = ( |
429 | | - resource.task |
430 | | - if resource.task is not None |
431 | | - else self._support_tasks_by_default |
432 | | - ) |
433 | | - if supports_task: |
| 413 | + if isinstance(resource, FunctionResource) and resource.task: |
434 | 414 | docket.register(resource.fn) |
435 | 415 |
|
436 | 416 | for template in self._resource_manager._templates.values(): |
437 | | - if not hasattr(template, "fn"): |
438 | | - continue |
439 | | - supports_task = ( |
440 | | - template.task |
441 | | - if template.task is not None |
442 | | - else self._support_tasks_by_default |
443 | | - ) |
444 | | - if supports_task: |
| 417 | + if isinstance(template, FunctionResourceTemplate) and template.task: |
445 | 418 | docket.register(template.fn) |
446 | 419 |
|
447 | 420 | # Set Docket in ContextVar so CurrentDocket can access it |
@@ -602,7 +575,11 @@ async def handler(req: mcp.types.ReadResourceRequest) -> mcp.types.ServerResult: |
602 | 575 | async with fastmcp.server.context.Context(fastmcp=self): |
603 | 576 | try: |
604 | 577 | resource = await self._resource_manager.get_resource(uri) |
605 | | - if resource and resource.task: |
| 578 | + if ( |
| 579 | + resource |
| 580 | + and isinstance(resource, FunctionResource) |
| 581 | + and resource.task |
| 582 | + ): |
606 | 583 | # Convert TaskMetadata to dict for handler |
607 | 584 | task_meta_dict = task_meta.model_dump(exclude_none=True) |
608 | 585 | return await handle_resource_as_task( |
@@ -671,7 +648,7 @@ async def handler(req: mcp.types.GetPromptRequest) -> mcp.types.ServerResult: |
671 | 648 | async with fastmcp.server.context.Context(fastmcp=self): |
672 | 649 | prompts = await self.get_prompts() |
673 | 650 | prompt = prompts.get(name) |
674 | | - if prompt and prompt.task: |
| 651 | + if prompt and isinstance(prompt, FunctionPrompt) and prompt.task: |
675 | 652 | # Convert TaskMetadata to dict for handler |
676 | 653 | task_meta_dict = task_meta.model_dump(exclude_none=True) |
677 | 654 | result = await handle_prompt_as_task( |
@@ -1349,7 +1326,7 @@ async def _call_tool_mcp( |
1349 | 1326 | if task_meta and fastmcp.settings.enable_tasks: |
1350 | 1327 | # Task metadata present - check if tool supports background execution |
1351 | 1328 | tool = self._tool_manager._tools.get(key) |
1352 | | - if tool and tool.task: |
| 1329 | + if tool and isinstance(tool, FunctionTool) and tool.task: |
1353 | 1330 | # Route to background execution |
1354 | 1331 | # Convert TaskMetadata to dict for handler |
1355 | 1332 | task_meta_dict = task_meta.model_dump(exclude_none=True) |
|
0 commit comments