Skip to content

Commit ce54c25

Browse files
authored
Fn to Invoke Resource (#1509)
* fix - uri_template will be appened to resource form only if uri is templatized resource else uri_template will be None (via mcpgateway UI) - admin.js Signed-off-by: Satya <tsp.0713@gmail.com> * added invoke resource, specific resource/resource template will be invoked and response will be returned whenever any resource is clicked on mcpgateway UI or mcp inspector Signed-off-by: Satya <tsp.0713@gmail.com> * pytests fixes minor change in invoke_resource Signed-off-by: Satya <tsp.0713@gmail.com> * pylint fixes Signed-off-by: Satya <tsp.0713@gmail.com> * moved the resources/prompts in streamablehttp_client outside tools block Signed-off-by: Satya <tsp.0713@gmail.com> --------- Signed-off-by: Satya <tsp.0713@gmail.com>
1 parent da5d067 commit ce54c25

File tree

5 files changed

+569
-80
lines changed

5 files changed

+569
-80
lines changed

mcpgateway/services/gateway_service.py

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,7 @@ async def _handle_gateway_failure(self, gateway: DbGateway) -> None:
20432043
None
20442044
20452045
Examples:
2046+
>>> from mcpgateway.services.gateway_service import GatewayService
20462047
>>> service = GatewayService()
20472048
>>> gateway = type('Gateway', (), {
20482049
... 'id': 'gw1', 'name': 'test_gw', 'enabled': True, 'reachable': True
@@ -3463,76 +3464,76 @@ def get_httpx_client_factory(
34633464
if tools:
34643465
logger.info(f"Fetched {len(tools)} tools from gateway")
34653466

3466-
# Fetch resources if supported
3467-
resources = []
3468-
logger.debug(f"Checking for resources support: {capabilities.get('resources')}")
3469-
if capabilities.get("resources"):
3470-
try:
3471-
response = await session.list_resources()
3472-
raw_resources = response.resources
3473-
for resource in raw_resources:
3474-
resource_data = resource.model_dump(by_alias=True, exclude_none=True)
3475-
# Convert AnyUrl to string if present
3476-
if "uri" in resource_data and hasattr(resource_data["uri"], "unicode_string"):
3477-
resource_data["uri"] = str(resource_data["uri"])
3478-
# Add default content if not present
3479-
if "content" not in resource_data:
3480-
resource_data["content"] = ""
3481-
try:
3482-
resources.append(ResourceCreate.model_validate(resource_data))
3483-
except Exception:
3484-
# If validation fails, create minimal resource
3485-
resources.append(
3486-
ResourceCreate(
3487-
uri=str(resource_data.get("uri", "")),
3488-
name=resource_data.get("name", ""),
3489-
description=resource_data.get("description"),
3490-
mime_type=resource_data.get("mimeType"),
3491-
uri_template=resource_data.get("uriTemplate") or None,
3492-
content="",
3493-
)
3467+
# Fetch resources if supported
3468+
resources = []
3469+
logger.debug(f"Checking for resources support: {capabilities.get('resources')}")
3470+
if capabilities.get("resources"):
3471+
try:
3472+
response = await session.list_resources()
3473+
raw_resources = response.resources
3474+
for resource in raw_resources:
3475+
resource_data = resource.model_dump(by_alias=True, exclude_none=True)
3476+
# Convert AnyUrl to string if present
3477+
if "uri" in resource_data and hasattr(resource_data["uri"], "unicode_string"):
3478+
resource_data["uri"] = str(resource_data["uri"])
3479+
# Add default content if not present
3480+
if "content" not in resource_data:
3481+
resource_data["content"] = ""
3482+
try:
3483+
resources.append(ResourceCreate.model_validate(resource_data))
3484+
except Exception:
3485+
# If validation fails, create minimal resource
3486+
resources.append(
3487+
ResourceCreate(
3488+
uri=str(resource_data.get("uri", "")),
3489+
name=resource_data.get("name", ""),
3490+
description=resource_data.get("description"),
3491+
mime_type=resource_data.get("mimeType"),
3492+
uri_template=resource_data.get("uriTemplate") or None,
3493+
content="",
34943494
)
3495-
logger.info(f"Fetched {len(resources)} resources from gateway")
3496-
except Exception as e:
3497-
logger.warning(f"Failed to fetch resources: {e}")
3498-
3499-
# resource template URI
3500-
try:
3501-
response_templates = await session.list_resource_templates()
3502-
raw_resources_templates = response_templates.resourceTemplates
3503-
resource_templates = []
3504-
for resource_template in raw_resources_templates:
3505-
resource_template_data = resource_template.model_dump(by_alias=True, exclude_none=True)
3506-
3507-
if "uriTemplate" in resource_template_data: # and hasattr(resource_template_data["uriTemplate"], "unicode_string"):
3508-
resource_template_data["uri_template"] = str(resource_template_data["uriTemplate"])
3509-
resource_template_data["uri"] = str(resource_template_data["uriTemplate"])
3510-
3511-
if "content" not in resource_template_data:
3512-
resource_template_data["content"] = ""
3495+
)
3496+
logger.info(f"Fetched {len(resources)} resources from gateway")
3497+
except Exception as e:
3498+
logger.warning(f"Failed to fetch resources: {e}")
35133499

3514-
resources.append(ResourceCreate.model_validate(resource_template_data))
3515-
resource_templates.append(ResourceCreate.model_validate(resource_template_data))
3516-
logger.info(f"Fetched {len(resource_templates)} resource templates from gateway")
3517-
except Exception as e:
3518-
logger.warning(f"Failed to fetch resource templates: {e}")
3500+
# resource template URI
3501+
try:
3502+
response_templates = await session.list_resource_templates()
3503+
raw_resources_templates = response_templates.resourceTemplates
3504+
resource_templates = []
3505+
for resource_template in raw_resources_templates:
3506+
resource_template_data = resource_template.model_dump(by_alias=True, exclude_none=True)
3507+
3508+
if "uriTemplate" in resource_template_data: # and hasattr(resource_template_data["uriTemplate"], "unicode_string"):
3509+
resource_template_data["uri_template"] = str(resource_template_data["uriTemplate"])
3510+
resource_template_data["uri"] = str(resource_template_data["uriTemplate"])
3511+
3512+
if "content" not in resource_template_data:
3513+
resource_template_data["content"] = ""
3514+
3515+
resources.append(ResourceCreate.model_validate(resource_template_data))
3516+
resource_templates.append(ResourceCreate.model_validate(resource_template_data))
3517+
logger.info(f"Fetched {len(resource_templates)} resource templates from gateway")
3518+
except Exception as e:
3519+
logger.warning(f"Failed to fetch resource templates: {e}")
35193520

3520-
# Fetch prompts if supported
3521-
prompts = []
3522-
logger.debug(f"Checking for prompts support: {capabilities.get('prompts')}")
3523-
if capabilities.get("prompts"):
3524-
try:
3525-
response = await session.list_prompts()
3526-
raw_prompts = response.prompts
3527-
for prompt in raw_prompts:
3528-
prompt_data = prompt.model_dump(by_alias=True, exclude_none=True)
3529-
# Add default template if not present
3530-
if "template" not in prompt_data:
3531-
prompt_data["template"] = ""
3532-
prompts.append(PromptCreate.model_validate(prompt_data))
3533-
logger.info(f"Fetched {len(prompts)} prompts from gateway")
3534-
except Exception as e:
3535-
logger.warning(f"Failed to fetch prompts: {e}")
3521+
# Fetch prompts if supported
3522+
prompts = []
3523+
logger.debug(f"Checking for prompts support: {capabilities.get('prompts')}")
3524+
if capabilities.get("prompts"):
3525+
try:
3526+
response = await session.list_prompts()
3527+
raw_prompts = response.prompts
3528+
for prompt in raw_prompts:
3529+
prompt_data = prompt.model_dump(by_alias=True, exclude_none=True)
3530+
# Add default template if not present
3531+
if "template" not in prompt_data:
3532+
prompt_data["template"] = ""
3533+
prompts.append(PromptCreate.model_validate(prompt_data))
3534+
logger.info(f"Fetched {len(prompts)} prompts from gateway")
3535+
except Exception as e:
3536+
logger.warning(f"Failed to fetch prompts: {e}")
35363537

35373538
return capabilities, tools, resources, prompts
35383539
raise GatewayConnectionError(f"Failed to initialize gateway at{server_url}")

0 commit comments

Comments
 (0)