In the previous parts, we created an MCP server with tools (Part 1) and looked at the transport layer, context, and the first resources (Part 2). Now we take it a step further: we add **context to resources**, create parameterized **resource templates**, and define reusable **prompts**.
⚠️ This chapter continues the code from the previous parts. Make sure you have the environment and server from parts 1 and 2.
Disclaimer: This post has been translated to English using a machine translation model. Please, let me know if you find any mistakes.
📚 **This entry is part of the _MCP with FastMCP_ series**, divided into four chapters that are read in order:
> * Part 1: First server and tools
* Part 2: Transport, context and resources
* 👉 **Part 3: Advanced resources and prompts**
* Part 4: HTTP, authentication, and client
Add context to the resource
As we did with the tools, we can add context to the resources.
MCP Server
InputPython%%writefile gitHub_MCP_server/github_server.pyimport httpxfrom fastmcp import FastMCP, Contextfrom github import GITHUB_TOKEN, create_github_headersUSER_ID = 1234567890# Create FastMCP servermcp = FastMCP(name="GitHubMCP",instructions="This server provides tools, resources and prompts to interact with the GitHub API.",include_tags={"public"},exclude_tags={"first_issue"})sub_mcp = FastMCP(name="SubMCP",)@mcp.tool(tags={"public", "production"},exclude_args=["user_id"], # user_id has to be injected by server, not provided by LLM)async def list_repository_issues(owner: str, repo_name: str, ctx: Context, user_id: int = USER_ID) -> list[dict]:"""Lists open issues for a given GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')ctx: The context of the requestuser_id: The user ID (automatically injected by the server)Returns:list[dict]: A list of dictionaries, each containing information about an issue"""# Limit to first 10 issues to avoid very long responsesapi_url = f"https://api.github.com/repos/{owner}/{repo_name}/issues?state=open&per_page=10"ctx.info(f"Fetching issues from {api_url}...")async with httpx.AsyncClient() as client:try:response = await client.get(api_url, headers=create_github_headers())response.raise_for_status()issues_data = response.json()if not issues_data:ctx.info("No open issues found for this repository.")return [{"message": "No open issues found for this repository."}]issues_summary = []for issue in issues_data:# Create a more concise summarysummary = f"#{issue.get('number', 'N/A')}: {issue.get('title', 'No title')}"if issue.get('comments', 0) > 0:summary += f" ({issue.get('comments')} comments)"issues_summary.append({"number": issue.get("number"),"title": issue.get("title"),"user": issue.get("user", {}).get("login"),"url": issue.get("html_url"),"comments": issue.get("comments"),"summary": summary})ctx.info(f"Found {len(issues_summary)} open issues.")# Add context informationresult = {"total_found": len(issues_summary),"repository": f"{owner}/{repo_name}","note": "Showing first 10 open issues" if len(issues_summary) == 10 else f"Showing all {len(issues_summary)} open issues","issues": issues_summary,"requested_by_user_id": user_id}return [result]except httpx.HTTPStatusError as e:error_message = e.response.json().get("message", "No additional message from API.")if e.response.status_code == 403 and GITHUB_TOKEN:error_message += " (Rate limit with token or token lacks permissions?)"elif e.response.status_code == 403 and not GITHUB_TOKEN:error_message += " (Rate limit without token. Consider creating a .env file with GITHUB_TOKEN.)"ctx.error(f"GitHub API error: {e.response.status_code}. {error_message}")return [{"error": f"GitHub API error: {e.response.status_code}","message": error_message}]except Exception as e:ctx.error(f"An unexpected error occurred: {str(e)}")return [{"error": f"An unexpected error occurred: {str(e)}"}]@mcp.tool(tags={"private", "development"})async def list_more_repository_issues(owner: str, repo_name: str) -> list[dict]:"""Lists open issues for a given GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')Returns:list[dict]: A list of dictionaries, each containing information about an issue"""# Limit to first 100 issues to avoid very long responsesapi_url = f"https://api.github.com/repos/{owner}/{repo_name}/issues?state=open&per_page=100"print(f"Fetching issues from {api_url}...")async with httpx.AsyncClient() as client:try:response = await client.get(api_url, headers=create_github_headers())response.raise_for_status()issues_data = response.json()if not issues_data:print("No open issues found for this repository.")return [{"message": "No open issues found for this repository."}]issues_summary = []for issue in issues_data:# Create a more concise summarysummary = f"#{issue.get('number', 'N/A')}: {issue.get('title', 'No title')}"if issue.get('comments', 0) > 0:summary += f" ({issue.get('comments')} comments)"issues_summary.append({"number": issue.get("number"),"title": issue.get("title"),"user": issue.get("user", {}).get("login"),"url": issue.get("html_url"),"comments": issue.get("comments"),"summary": summary})print(f"Found {len(issues_summary)} open issues.")# Add context informationresult = {"total_found": len(issues_summary),"repository": f"{owner}/{repo_name}","note": "Showing first 10 open issues" if len(issues_summary) == 10 else f"Showing all {len(issues_summary)} open issues","issues": issues_summary}return [result]except httpx.HTTPStatusError as e:error_message = e.response.json().get("message", "No additional message from API.")if e.response.status_code == 403 and GITHUB_TOKEN:error_message += " (Rate limit with token or token lacks permissions?)"elif e.response.status_code == 403 and not GITHUB_TOKEN:error_message += " (Rate limit without token. Consider creating a .env file with GITHUB_TOKEN.)"print(f"GitHub API error: {e.response.status_code}. {error_message}")return [{"error": f"GitHub API error: {e.response.status_code}","message": error_message}]except Exception as e:print(f"An unexpected error occurred: {str(e)}")return [{"error": f"An unexpected error occurred: {str(e)}"}]@mcp.tool(tags={"public", "first_issue"})async def first_repository_issue(owner: str, repo_name: str) -> list[dict]:"""Gets the first issue ever created in a GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')Returns:list[dict]: A list containing information about the first issue created"""# Get the first issue by sorting by creation date in ascending orderapi_url = f"https://api.github.com/repos/{owner}/{repo_name}/issues?state=all&sort=created&direction=asc&per_page=1"print(f"Fetching first issue from {api_url}...")async with httpx.AsyncClient() as client:try:response = await client.get(api_url, headers=create_github_headers())response.raise_for_status()issues_data = response.json()if not issues_data:print("No issues found for this repository.")return [{"message": "No issues found for this repository."}]first_issue = issues_data[0]# Create a detailed summary of the first issuesummary = f"#{first_issue.get('number', 'N/A')}: {first_issue.get('title', 'No title')}"if first_issue.get('comments', 0) > 0:summary += f" ({first_issue.get('comments')} comments)"issue_info = {"number": first_issue.get("number"),"title": first_issue.get("title"),"user": first_issue.get("user", {}).get("login"),"url": first_issue.get("html_url"),"state": first_issue.get("state"),"comments": first_issue.get("comments"),"created_at": first_issue.get("created_at"),"updated_at": first_issue.get("updated_at"),"body": first_issue.get("body", "")[:500] + "..." if len(first_issue.get("body", "")) > 500 else first_issue.get("body", ""),"summary": summary}print(f"Found first issue: #{first_issue.get('number')} created on {first_issue.get('created_at')}")# Add context informationresult = {"repository": f"{owner}/{repo_name}","note": "This is the very first issue created in this repository","first_issue": issue_info}return [result]except httpx.HTTPStatusError as e:error_message = e.response.json().get("message", "No additional message from API.")if e.response.status_code == 403 and GITHUB_TOKEN:error_message += " (Rate limit with token or token lacks permissions?)"elif e.response.status_code == 403 and not GITHUB_TOKEN:error_message += " (Rate limit without token. Consider creating a .env file with GITHUB_TOKEN.)"print(f"GitHub API error: {e.response.status_code}. {error_message}")return [{"error": f"GitHub API error: {e.response.status_code}","message": error_message}]except Exception as e:print(f"An unexpected error occurred: {str(e)}")return [{"error": f"An unexpected error occurred: {str(e)}"}]@mcp.resource("resource://server_info", tags={"public"})def server_info(ctx: Context) -> str:"""Returns information about the server."""return {"info": "This is the MCP GitHub server development for MaximoFN blog post","requested_id": ctx.request_id}@sub_mcp.tool(tags={"public"})def hello_world() -> str:"""Returns a simple greeting."""return "Hello, world!"mcp.mount("sub_mcp", sub_mcp)if __name__ == "__main__":print("DEBUG: Starting FastMCP GitHub server...")print(f"DEBUG: Server name: {mcp.name}")# Initialize and run the servermcp.run(transport="stdio")Copied
Overwriting gitHub_MCP_server/github_server.py
We have added context to the resource server_info so that it returns the request ID.
return {
"info": "This is the development of the MCP GitHub server for the MaximoFN blog post",
"requested_id": ctx.request_id
}Server test with context in the resource
We run the client
InputPython!cd client_MCP && source .venv/bin/activate && python client.py ../gitHub_MCP_server/github_server.pyCopied
🔗 Connecting to FastMCP server: ../gitHub_MCP_server/github_server.py✅ Client created successfully/Users/macm1/Documents/web/portafolio/posts/gitHub_MCP_server/github_server.py:258: DeprecationWarning: Mount prefixes are now optional and the first positional argument should be the server you want to mount.mcp.mount("sub_mcp", sub_mcp)[06/28/25 11:17:41] INFO Starting MCP server 'GitHubMCP' with transport 'stdio' server.py:1246🛠️ Available tools (2):==================================================📋 sub_mcp_hello_worldDescription: Returns a simple greeting.Parameters:📋 list_repository_issuesDescription: Lists open issues for a given GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')ctx: The context of the requestuser_id: The user ID (automatically injected by the server)Returns:list[dict]: A list of dictionaries, each containing information about an issueParameters: owner, repo_name...🔧 Claude wants to use: read_mcp_resource📝 Arguments: {'resource_uri': 'resource://server_info'}📖 Resource read successfully: resource://server_info🤖 Claude: I'll help you read the server information using the `read_mcp_resource` function. The server information is available at the resource URI "resource://server_info".According to the server information:- This is the MCP GitHub server development for MaximoFN blog post- The requested ID is "7"👤 You: q👋 Bye!🧹 Cleaning up resources...✅ Resources released
As we can see, it has given us the server information and the request ID.
👤 You: Tell me the server info
🤔 Claude is thinking...
🔧 Claude wants to use: read_mcp_resource
📝 Arguments: {'resource_uri': 'resource://server_info'}
📖 Resource read successfully: resource://server_info
🤖 Claude: Te ayudaré a leer la información del servidor usando la función `read_mcp_resource`. La información del servidor está disponible en la URI del recurso "resource://server_info".Según la información del servidor:
- Esta es la publicación del blog de MaximoFN sobre el desarrollo del servidor MCP de GitHub
- The requested ID is "7"Create a resource template
Before we created a resource, which is a static resource, but maybe we want to obtain information, but not always the same information; we want the LLM to be able to decide what information it wants or needs.
For this, we have the resource templates, which give us information just like a resource, but dynamically. At the time of the request, the resource is created and returned.
MCP Server
Creating a resource template is done in the same way as creating a resource, that is, using @mcp.resource(<ENDPOINT), only now the endpoint is a template that is filled in at the time of the request.
Let's see it.
InputPython%%writefile gitHub_MCP_server/github_server.pyimport httpxfrom fastmcp import FastMCP, Contextfrom github import GITHUB_TOKEN, create_github_headersimport datetimeUSER_ID = 1234567890# Create FastMCP servermcp = FastMCP(name="GitHubMCP",instructions="This server provides tools, resources and prompts to interact with the GitHub API.",include_tags={"public"},exclude_tags={"first_issue"})sub_mcp = FastMCP(name="SubMCP",)@mcp.tool(tags={"public", "production"},exclude_args=["user_id"], # user_id has to be injected by server, not provided by LLM)async def list_repository_issues(owner: str, repo_name: str, ctx: Context, user_id: int = USER_ID) -> list[dict]:"""Lists open issues for a given GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')ctx: The context of the requestuser_id: The user ID (automatically injected by the server)Returns:list[dict]: A list of dictionaries, each containing information about an issue"""# Limit to first 10 issues to avoid very long responsesapi_url = f"https://api.github.com/repos/{owner}/{repo_name}/issues?state=open&per_page=10"ctx.info(f"Fetching issues from {api_url}...")async with httpx.AsyncClient() as client:try:response = await client.get(api_url, headers=create_github_headers())response.raise_for_status()issues_data = response.json()if not issues_data:ctx.info("No open issues found for this repository.")return [{"message": "No open issues found for this repository."}]issues_summary = []for issue in issues_data:# Create a more concise summarysummary = f"#{issue.get('number', 'N/A')}: {issue.get('title', 'No title')}"if issue.get('comments', 0) > 0:summary += f" ({issue.get('comments')} comments)"issues_summary.append({"number": issue.get("number"),"title": issue.get("title"),"user": issue.get("user", {}).get("login"),"url": issue.get("html_url"),"comments": issue.get("comments"),"summary": summary})ctx.info(f"Found {len(issues_summary)} open issues.")# Add context informationresult = {"total_found": len(issues_summary),"repository": f"{owner}/{repo_name}","note": "Showing first 10 open issues" if len(issues_summary) == 10 else f"Showing all {len(issues_summary)} open issues","issues": issues_summary,"requested_by_user_id": user_id}return [result]except httpx.HTTPStatusError as e:error_message = e.response.json().get("message", "No additional message from API.")if e.response.status_code == 403 and GITHUB_TOKEN:error_message += " (Rate limit with token or token lacks permissions?)"elif e.response.status_code == 403 and not GITHUB_TOKEN:error_message += " (Rate limit without token. Consider creating a .env file with GITHUB_TOKEN.)"ctx.error(f"GitHub API error: {e.response.status_code}. {error_message}")return [{"error": f"GitHub API error: {e.response.status_code}","message": error_message}]except Exception as e:ctx.error(f"An unexpected error occurred: {str(e)}")return [{"error": f"An unexpected error occurred: {str(e)}"}]@mcp.tool(tags={"private", "development"})async def list_more_repository_issues(owner: str, repo_name: str) -> list[dict]:"""Lists open issues for a given GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')Returns:list[dict]: A list of dictionaries, each containing information about an issue"""# Limit to first 100 issues to avoid very long responsesapi_url = f"https://api.github.com/repos/{owner}/{repo_name}/issues?state=open&per_page=100"print(f"Fetching issues from {api_url}...")async with httpx.AsyncClient() as client:try:response = await client.get(api_url, headers=create_github_headers())response.raise_for_status()issues_data = response.json()if not issues_data:print("No open issues found for this repository.")return [{"message": "No open issues found for this repository."}]issues_summary = []for issue in issues_data:# Create a more concise summarysummary = f"#{issue.get('number', 'N/A')}: {issue.get('title', 'No title')}"if issue.get('comments', 0) > 0:summary += f" ({issue.get('comments')} comments)"issues_summary.append({"number": issue.get("number"),"title": issue.get("title"),"user": issue.get("user", {}).get("login"),"url": issue.get("html_url"),"comments": issue.get("comments"),"summary": summary})print(f"Found {len(issues_summary)} open issues.")# Add context informationresult = {"total_found": len(issues_summary),"repository": f"{owner}/{repo_name}","note": "Showing first 10 open issues" if len(issues_summary) == 10 else f"Showing all {len(issues_summary)} open issues","issues": issues_summary}return [result]except httpx.HTTPStatusError as e:error_message = e.response.json().get("message", "No additional message from API.")if e.response.status_code == 403 and GITHUB_TOKEN:error_message += " (Rate limit with token or token lacks permissions?)"elif e.response.status_code == 403 and not GITHUB_TOKEN:error_message += " (Rate limit without token. Consider creating a .env file with GITHUB_TOKEN.)"print(f"GitHub API error: {e.response.status_code}. {error_message}")return [{"error": f"GitHub API error: {e.response.status_code}","message": error_message}]except Exception as e:print(f"An unexpected error occurred: {str(e)}")return [{"error": f"An unexpected error occurred: {str(e)}"}]@mcp.tool(tags={"public", "first_issue"})async def first_repository_issue(owner: str, repo_name: str) -> list[dict]:"""Gets the first issue ever created in a GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')Returns:list[dict]: A list containing information about the first issue created"""# Get the first issue by sorting by creation date in ascending orderapi_url = f"https://api.github.com/repos/{owner}/{repo_name}/issues?state=all&sort=created&direction=asc&per_page=1"print(f"Fetching first issue from {api_url}...")async with httpx.AsyncClient() as client:try:response = await client.get(api_url, headers=create_github_headers())response.raise_for_status()issues_data = response.json()if not issues_data:print("No issues found for this repository.")return [{"message": "No issues found for this repository."}]first_issue = issues_data[0]# Create a detailed summary of the first issuesummary = f"#{first_issue.get('number', 'N/A')}: {first_issue.get('title', 'No title')}"if first_issue.get('comments', 0) > 0:summary += f" ({first_issue.get('comments')} comments)"issue_info = {"number": first_issue.get("number"),"title": first_issue.get("title"),"user": first_issue.get("user", {}).get("login"),"url": first_issue.get("html_url"),"state": first_issue.get("state"),"comments": first_issue.get("comments"),"created_at": first_issue.get("created_at"),"updated_at": first_issue.get("updated_at"),"body": first_issue.get("body", "")[:500] + "..." if len(first_issue.get("body", "")) > 500 else first_issue.get("body", ""),"summary": summary}print(f"Found first issue: #{first_issue.get('number')} created on {first_issue.get('created_at')}")# Add context informationresult = {"repository": f"{owner}/{repo_name}","note": "This is the very first issue created in this repository","first_issue": issue_info}return [result]except httpx.HTTPStatusError as e:error_message = e.response.json().get("message", "No additional message from API.")if e.response.status_code == 403 and GITHUB_TOKEN:error_message += " (Rate limit with token or token lacks permissions?)"elif e.response.status_code == 403 and not GITHUB_TOKEN:error_message += " (Rate limit without token. Consider creating a .env file with GITHUB_TOKEN.)"print(f"GitHub API error: {e.response.status_code}. {error_message}")return [{"error": f"GitHub API error: {e.response.status_code}","message": error_message}]except Exception as e:print(f"An unexpected error occurred: {str(e)}")return [{"error": f"An unexpected error occurred: {str(e)}"}]@mcp.resource("resource://server_info", tags={"public"})def server_info(ctx: Context) -> str:"""Returns information about the server."""return {"info": "This is the MCP GitHub server development for MaximoFN blog post","requested_id": ctx.request_id}@mcp.resource("github://repo/{owner}/{repo_name}", tags={"public"})async def repository_info(owner: str, repo_name: str, ctx: Context) -> dict:"""Returns detailed information about a GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')ctx: The context of the requestReturns:dict: Repository information including name, description, stats, etc."""api_url = f"https://api.github.com/repos/{owner}/{repo_name}"ctx.info(f"Fetching repository information from {api_url}...")async with httpx.AsyncClient() as client:try:response = await client.get(api_url, headers=create_github_headers())response.raise_for_status()repo_data = response.json()# Extract relevant repository informationrepo_info = {"name": repo_data.get("name"),"full_name": repo_data.get("full_name"),"description": repo_data.get("description"),"owner": {"login": repo_data.get("owner", {}).get("login"),"type": repo_data.get("owner", {}).get("type")},"html_url": repo_data.get("html_url"),"clone_url": repo_data.get("clone_url"),"ssh_url": repo_data.get("ssh_url"),"language": repo_data.get("language"),"size": repo_data.get("size"), # Size in KB"stargazers_count": repo_data.get("stargazers_count"),"watchers_count": repo_data.get("watchers_count"),"forks_count": repo_data.get("forks_count"),"open_issues_count": repo_data.get("open_issues_count"),"default_branch": repo_data.get("default_branch"),"created_at": repo_data.get("created_at"),"updated_at": repo_data.get("updated_at"),"pushed_at": repo_data.get("pushed_at"),"is_private": repo_data.get("private"),"is_fork": repo_data.get("fork"),"is_archived": repo_data.get("archived"),"has_issues": repo_data.get("has_issues"),"has_projects": repo_data.get("has_projects"),"has_wiki": repo_data.get("has_wiki"),"license": repo_data.get("license", {}).get("name") if repo_data.get("license") else None,"topics": repo_data.get("topics", [])}ctx.info(f"Successfully retrieved information for repository {owner}/{repo_name}")return repo_infoexcept httpx.HTTPStatusError as e:error_message = e.response.json().get("message", "No additional message from API.")if e.response.status_code == 404:error_message = f"Repository {owner}/{repo_name} not found or is private."elif e.response.status_code == 403 and GITHUB_TOKEN:error_message += " (Rate limit with token or token lacks permissions?)"elif e.response.status_code == 403 and not GITHUB_TOKEN:error_message += " (Rate limit without token. Consider creating a .env file with GITHUB_TOKEN.)"ctx.error(f"GitHub API error: {e.response.status_code}. {error_message}")return {"error": f"GitHub API error: {e.response.status_code}","message": error_message,"repository": f"{owner}/{repo_name}"}except Exception as e:ctx.error(f"An unexpected error occurred: {str(e)}")return {"error": f"An unexpected error occurred: {str(e)}","repository": f"{owner}/{repo_name}"}@sub_mcp.tool(tags={"public"})def hello_world() -> str:"""Returns a simple greeting."""return "Hello, world!"mcp.mount("sub_mcp", sub_mcp)if __name__ == "__main__":print("DEBUG: Starting FastMCP GitHub server...")print(f"DEBUG: Server name: {mcp.name}")# Initialize and run the servermcp.run(transport="stdio")Copied
Overwriting gitHub_MCP_server/github_server.py
We have created the resource template repository_info, which provides us with information about a repository that will determine the LLM. The template is created and, at runtime, it is filled in with the parameters passed to it.
@mcp.resource("github://repo/{owner}/{repo_name}", tags={"public"})
async def repository_info(owner: str, repo_name: str, ctx: Context) -> dict:Both the repository and the repository owner must be parameters of the function.
Client MCP
We make a small change in the client so that the LLM understands that there are static and dynamic resources.
InputPython%%writefile client_MCP/client.pyimport sysimport asynciofrom contextlib import AsyncExitStackfrom anthropic import Anthropicfrom dotenv import load_dotenvfrom fastmcp import Client# Load environment variables from .env fileload_dotenv()class FastMCPClient:"""FastMCP client that integrates with Claude to process user queriesand use tools and resources exposed by a FastMCP server."""def __init__(self):"""Initialize the FastMCP client with Anthropic and resource management."""self.exit_stack = AsyncExitStack()self.anthropic = Anthropic()self.client = Noneasync def connect_to_server(self, server_script_path: str):"""Connect to the specified FastMCP server.Args:server_script_path: Path to the server script (Python)"""print(f"🔗 Connecting to FastMCP server: {server_script_path}")# Determine the server type based on the extensionif not server_script_path.endswith('.py'):raise ValueError(f"Unsupported server type. Use .py files. Received: {server_script_path}")# Create FastMCP clientself.client = Client(server_script_path)# Note: FastMCP Client automatically infers transport from .py filesprint("✅ Client created successfully")async def list_available_tools(self):"""List available tools in the FastMCP server."""try:# Get list of tools from the server using FastMCP contextasync with self.client as client:tools = await client.list_tools()if tools:print(f" 🛠️ Available tools ({len(tools)}):")print("=" * 50)for tool in tools:print(f"📋 {tool.name}")if tool.description:print(f" Description: {tool.description}")# Show parameters if availableif hasattr(tool, 'inputSchema') and tool.inputSchema:if 'properties' in tool.inputSchema:params = list(tool.inputSchema['properties'].keys())print(f" Parameters: {', '.join(params)}")print()else:print("⚠️ No tools found in the server")except Exception as e:print(f"❌ Error listing tools: {str(e)}")async def list_available_resources(self):"""List available resources in the FastMCP server."""try:# Get list of resources from the server using FastMCP contextasync with self.client as client:resources = await client.list_resources()if resources:print(f" 📚 Available resources ({len(resources)}):")print("=" * 50)for resource in resources:print(f"📄 {resource.uri}")if resource.name:print(f" Name: {resource.name}")if resource.description:print(f" Description: {resource.description}")if resource.mimeType:print(f" MIME Type: {resource.mimeType}")print()else:print("⚠️ No resources found in the server")except Exception as e:print(f"❌ Error listing resources: {str(e)}")async def read_resource(self, resource_uri: str):"""Read a specific resource from the server.Args:resource_uri: URI of the resource to readReturns:str: Resource content"""try:async with self.client as client:result = await client.read_resource(resource_uri)return resultexcept Exception as e:print(f"❌ Error reading resource {resource_uri}: {str(e)}")return Noneasync def process_query(self, query: str) -> str:"""Process a user query, interacting with Claude and FastMCP tools and resources.Args:query: User queryReturns:str: Final processed response"""try:# Use FastMCP context for all operationsasync with self.client as client:# Get available tools and resourcestools_list = await client.list_tools()resources_list = await client.list_resources()# Prepare tools for Claude in correct formatclaude_tools = []for tool in tools_list:claude_tool = {"name": tool.name,"description": tool.description or f"Tool {tool.name}","input_schema": tool.inputSchema or {"type": "object", "properties": {}}}claude_tools.append(claude_tool)# Add a special tool for reading resources (including template resources)resource_description = "Read a resource from the MCP server. "if resources_list:# Convert URIs to strings to avoid AnyUrl object issuesresource_uris = [str(r.uri) for r in resources_list]resource_description += f"Available static resources: {', '.join(resource_uris)}. "resource_description += "Also supports template resources like github://repo/owner/repo_name for GitHub repository information."claude_tools.append({"name": "read_mcp_resource","description": resource_description,"input_schema": {"type": "object","properties": {"resource_uri": {"type": "string","description": "URI of the resource to read. Can be static (like resource://server_info) or template-based (like github://repo/facebook/react)"}},"required": ["resource_uri"]}})# Create initial message for Claudemessages = [{"role": "user","content": query}]# First call to Clauderesponse = self.anthropic.messages.create(model="claude-3-5-sonnet-20241022",max_tokens=6000,messages=messages,tools=claude_tools if claude_tools else None)# Process Claude's responseresponse_text = ""for content_block in response.content:if content_block.type == "text":response_text += content_block.textelif content_block.type == "tool_use":# Claude wants to use a tooltool_name = content_block.nametool_args = content_block.inputtool_call_id = content_block.idprint(f"🔧 Claude wants to use: {tool_name}")print(f"📝 Arguments: {tool_args}")try:if tool_name == "read_mcp_resource":# Handle resource readingresource_uri = tool_args.get("resource_uri")if resource_uri:tool_result = await client.read_resource(resource_uri)print(f"📖 Resource read successfully: {resource_uri}")# Better handling of resource resultif hasattr(tool_result, 'content'):# If it's a resource response object, extract contentif hasattr(tool_result.content, 'text'):result_content = tool_result.content.textelse:result_content = str(tool_result.content)else:# If it's already a string or simple objectresult_content = str(tool_result)else:tool_result = "Error: No resource URI provided"result_content = tool_resultelse:# Execute regular tool on the FastMCP servertool_result = await client.call_tool(tool_name, tool_args)print(f"✅ Tool executed successfully")result_content = str(tool_result)# Add tool result to the conversationmessages.append({"role": "assistant","content": response.content})# Format result for Claudeif tool_result:messages.append({"role": "user","content": [{"type": "tool_result","tool_use_id": tool_call_id,"content": f"Tool result: {result_content}"}]})else:messages.append({"role": "user","content": [{"type": "tool_result","tool_use_id": tool_call_id,"content": "Tool executed without response content"}]})# Second call to Claude with the tool resultfinal_response = self.anthropic.messages.create(model="claude-3-5-sonnet-20241022",max_tokens=6000,messages=messages,tools=claude_tools if claude_tools else None)# Extract text from the final responsefor final_content in final_response.content:if final_content.type == "text":response_text += final_content.textexcept Exception as e:error_msg = f"❌ Error executing {tool_name}: {str(e)}"print(error_msg)response_text += f" {error_msg}"return response_textexcept Exception as e:error_msg = f"❌ Error processing query: {str(e)}"print(error_msg)return error_msgasync def chat_loop(self):"""Main chat loop with user interaction."""print(" 🤖 FastMCP client started. Write 'quit', 'q', 'exit', 'salir' to exit.")print("💬 You can ask questions about GitHub repositories!")print("📚 The client can use tools and resources from the FastMCP server")print("-" * 60)while True:try:# Request user inputuser_input = input(" 👤 You: ").strip()if user_input.lower() in ['quit', 'q', 'exit', 'salir']:print("👋 Bye!")breakif not user_input:continueprint(" 🤔 Claude is thinking...")# Process queryresponse = await self.process_query(user_input)# Show responseprint(f" 🤖 Claude: {response}")except KeyboardInterrupt:print(" 👋 Disconnecting...")breakexcept Exception as e:print(f" ❌ Error in chat: {str(e)}")continueasync def cleanup(self):"""Clean up resources and close connections."""print("🧹 Cleaning up resources...")# FastMCP Client cleanup is handled automatically by context managerawait self.exit_stack.aclose()print("✅ Resources released")async def main():"""Main function that initializes and runs the FastMCP client."""# Verify command line argumentsif len(sys.argv) != 2:print("❌ Usage: python client.py <path_to_fastmcp_server>")print("📝 Example: python client.py ../MCP_github/github_server.py")sys.exit(1)server_script_path = sys.argv[1]# Create and run clientclient = FastMCPClient()try:# Connect to the serverawait client.connect_to_server(server_script_path)# List available tools and resources after connectionawait client.list_available_tools()await client.list_available_resources()# Start chat loopawait client.chat_loop()except Exception as e:print(f"❌ Fatal error: {str(e)}")finally:# Ensure resources are cleaned upawait client.cleanup()if __name__ == "__main__":# Entry point of the scriptasyncio.run(main())Copied
Overwriting client_MCP/client.py
As we see, we tell it "description": "URI of the resource to read. Can be static (like resource://server_info) or template-based (like github://repo/facebook/react)"
claude_tools.append({
"name": "read_mcp_resource",
"description": "Read a resource from the MCP server. Available resources: " +
", ".join(resource_uris),
"input_schema": {
"type": "object",json
"properties":
We run the client "resource_uri": { "type": "string",
"description": "URI of the resource to read"
}
},
"required": ["resource_uri"]
}
})resource template test
InputPython!cd client_MCP && source .venv/bin/activate && python client.py ../gitHub_MCP_server/github_server.pyCopied
🔗 Connecting to FastMCP server: ../gitHub_MCP_server/github_server.py✅ Client created successfully🛠️ Available tools (2):==================================================📋 sub_mcp_hello_worldDescription: Returns a simple greeting.Parameters:📋 list_repository_issuesDescription: Lists open issues for a given GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')ctx: The context of the requestuser_id: The user ID (automatically injected by the server)Returns:list[dict]: A list of dictionaries, each containing information about an issueParameters: owner, repo_name📚 Available resources (1):==================================================📄 resource://server_info...- react- uiThe repository can be accessed via:- HTTPS: https://github.com/facebook/react- SSH: git@github.com:facebook/react.gitThis is one of the most popular repositories on GitHub, as evidenced by its high number of stars and forks, and it remains actively maintained with regular updates.👤 You: q👋 Bye!🧹 Cleaning up resources...✅ Resources released
We request the information from a repository; use the resource template repository_info and it gives us the repository information.
👤 You: Can you read the resource github://repo/facebook/react for detailed information about the repository?
🤔 Claude is thinking...
🔧 Claude wants to use: read_mcp_resource
📝 Arguments: {'resource_uri': 'github://repo/facebook/react'}
📖 Resource read successfully: github://repo/facebook/react
🤖 Claude: I'll help you read the GitHub repository information for Facebook's React using the `read_mcp_resource` function.Based on the repository information retrieved, here are the key details about the Facebook React repository:
1. Description: The library for web and native user interfaces
2. Owner: Facebook (Organization)
3. Language: JavaScript
4. Repository Statistics:
- Stars: 236,803
- Forks: 48,815
- Open Issues: 999
- Watchers: 236,803
5. Important Dates:
- Created: May 24, 2013
- Last Updated: June 28, 2025
- Último push: 27 de junio de 2025
6. Funciones del repositorio:
- Repositorio público (no privado)
- Not a fork- Not archived
- Has issues enabled
- Projects disabled
- Wiki disabled
7. License: MIT License
8. Topics/Tags:
- declarative
- frontend
- javascript
- library
- react
- ui
The repository can be accessed via:
- HTTPS: https://github.com/facebook/react
- SSH: git@github.com:facebook/react.git
This is one of the most popular repositories on GitHub, as evidenced by its high number of stars and forks, and it remains actively maintained with regular updates.Create a prompt
Another of the tools that MCP offers us is asking the LLM to create a prompt for us to use in a request.
MCP Server
We create a prompt on our server; for this, we use the @mcp.prompt decorator and pass it the prompt name, the description, and the public tag, because we had defined our server to only include the tools, resources, and prompts with the public tag.
@mcp.prompt(
name="generate_issues_prompt",
description="Generates a structured prompt for asking about GitHub repository issues. Use this when users want to formulate questions about repository issues, or need help creating prompts for issue analysis.",tags={"public"}
)InputPython%%writefile gitHub_MCP_server/github_server.pyimport httpxfrom fastmcp import FastMCP, Contextfrom github import GITHUB_TOKEN, create_github_headersimport datetimeUSER_ID = 1234567890# Create FastMCP servermcp = FastMCP(name="GitHubMCP",instructions="This server provides tools, resources and prompts to interact with the GitHub API.",include_tags={"public"},exclude_tags={"first_issue"})sub_mcp = FastMCP(name="SubMCP",)@mcp.tool(tags={"public", "production"},exclude_args=["user_id"], # user_id has to be injected by server, not provided by LLM)async def list_repository_issues(owner: str, repo_name: str, ctx: Context, user_id: int = USER_ID) -> list[dict]:"""Lists open issues for a given GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')ctx: The context of the requestuser_id: The user ID (automatically injected by the server)Returns:list[dict]: A list of dictionaries, each containing information about an issue"""# Limit to first 10 issues to avoid very long responsesapi_url = f"https://api.github.com/repos/{owner}/{repo_name}/issues?state=open&per_page=10"ctx.info(f"Fetching issues from {api_url}...")async with httpx.AsyncClient() as client:try:response = await client.get(api_url, headers=create_github_headers())response.raise_for_status()issues_data = response.json()if not issues_data:ctx.info("No open issues found for this repository.")return [{"message": "No open issues found for this repository."}]issues_summary = []for issue in issues_data:# Create a more concise summarysummary = f"#{issue.get('number', 'N/A')}: {issue.get('title', 'No title')}"if issue.get('comments', 0) > 0:summary += f" ({issue.get('comments')} comments)"issues_summary.append({"number": issue.get("number"),"title": issue.get("title"),"user": issue.get("user", {}).get("login"),"url": issue.get("html_url"),"comments": issue.get("comments"),"summary": summary})ctx.info(f"Found {len(issues_summary)} open issues.")# Add context informationresult = {"total_found": len(issues_summary),"repository": f"{owner}/{repo_name}","note": "Showing first 10 open issues" if len(issues_summary) == 10 else f"Showing all {len(issues_summary)} open issues","issues": issues_summary,"requested_by_user_id": user_id}return [result]except httpx.HTTPStatusError as e:error_message = e.response.json().get("message", "No additional message from API.")if e.response.status_code == 403 and GITHUB_TOKEN:error_message += " (Rate limit with token or token lacks permissions?)"elif e.response.status_code == 403 and not GITHUB_TOKEN:error_message += " (Rate limit without token. Consider creating a .env file with GITHUB_TOKEN.)"ctx.error(f"GitHub API error: {e.response.status_code}. {error_message}")return [{"error": f"GitHub API error: {e.response.status_code}","message": error_message}]except Exception as e:ctx.error(f"An unexpected error occurred: {str(e)}")return [{"error": f"An unexpected error occurred: {str(e)}"}]@mcp.tool(tags={"private", "development"})async def list_more_repository_issues(owner: str, repo_name: str) -> list[dict]:"""Lists open issues for a given GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')Returns:list[dict]: A list of dictionaries, each containing information about an issue"""# Limit to first 100 issues to avoid very long responsesapi_url = f"https://api.github.com/repos/{owner}/{repo_name}/issues?state=open&per_page=100"print(f"Fetching issues from {api_url}...")async with httpx.AsyncClient() as client:try:response = await client.get(api_url, headers=create_github_headers())response.raise_for_status()issues_data = response.json()if not issues_data:print("No open issues found for this repository.")return [{"message": "No open issues found for this repository."}]issues_summary = []for issue in issues_data:# Create a more concise summarysummary = f"#{issue.get('number', 'N/A')}: {issue.get('title', 'No title')}"if issue.get('comments', 0) > 0:summary += f" ({issue.get('comments')} comments)"issues_summary.append({"number": issue.get("number"),"title": issue.get("title"),"user": issue.get("user", {}).get("login"),"url": issue.get("html_url"),"comments": issue.get("comments"),"summary": summary})print(f"Found {len(issues_summary)} open issues.")# Add context informationresult = {"total_found": len(issues_summary),"repository": f"{owner}/{repo_name}","note": "Showing first 10 open issues" if len(issues_summary) == 10 else f"Showing all {len(issues_summary)} open issues","issues": issues_summary}return [result]except httpx.HTTPStatusError as e:error_message = e.response.json().get("message", "No additional message from API.")if e.response.status_code == 403 and GITHUB_TOKEN:error_message += " (Rate limit with token or token lacks permissions?)"elif e.response.status_code == 403 and not GITHUB_TOKEN:error_message += " (Rate limit without token. Consider creating a .env file with GITHUB_TOKEN.)"print(f"GitHub API error: {e.response.status_code}. {error_message}")return [{"error": f"GitHub API error: {e.response.status_code}","message": error_message}]except Exception as e:print(f"An unexpected error occurred: {str(e)}")return [{"error": f"An unexpected error occurred: {str(e)}"}]@mcp.tool(tags={"public", "first_issue"})async def first_repository_issue(owner: str, repo_name: str) -> list[dict]:"""Gets the first issue ever created in a GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')Returns:list[dict]: A list containing information about the first issue created"""# Get the first issue by sorting by creation date in ascending orderapi_url = f"https://api.github.com/repos/{owner}/{repo_name}/issues?state=all&sort=created&direction=asc&per_page=1"print(f"Fetching first issue from {api_url}...")async with httpx.AsyncClient() as client:try:response = await client.get(api_url, headers=create_github_headers())response.raise_for_status()issues_data = response.json()if not issues_data:print("No issues found for this repository.")return [{"message": "No issues found for this repository."}]first_issue = issues_data[0]# Create a detailed summary of the first issuesummary = f"#{first_issue.get('number', 'N/A')}: {first_issue.get('title', 'No title')}"if first_issue.get('comments', 0) > 0:summary += f" ({first_issue.get('comments')} comments)"issue_info = {"number": first_issue.get("number"),"title": first_issue.get("title"),"user": first_issue.get("user", {}).get("login"),"url": first_issue.get("html_url"),"state": first_issue.get("state"),"comments": first_issue.get("comments"),"created_at": first_issue.get("created_at"),"updated_at": first_issue.get("updated_at"),"body": first_issue.get("body", "")[:500] + "..." if len(first_issue.get("body", "")) > 500 else first_issue.get("body", ""),"summary": summary}print(f"Found first issue: #{first_issue.get('number')} created on {first_issue.get('created_at')}")# Add context informationresult = {"repository": f"{owner}/{repo_name}","note": "This is the very first issue created in this repository","first_issue": issue_info}return [result]except httpx.HTTPStatusError as e:error_message = e.response.json().get("message", "No additional message from API.")if e.response.status_code == 403 and GITHUB_TOKEN:error_message += " (Rate limit with token or token lacks permissions?)"elif e.response.status_code == 403 and not GITHUB_TOKEN:error_message += " (Rate limit without token. Consider creating a .env file with GITHUB_TOKEN.)"print(f"GitHub API error: {e.response.status_code}. {error_message}")return [{"error": f"GitHub API error: {e.response.status_code}","message": error_message}]except Exception as e:print(f"An unexpected error occurred: {str(e)}")return [{"error": f"An unexpected error occurred: {str(e)}"}]@mcp.resource("resource://server_info", tags={"public"})def server_info(ctx: Context) -> str:"""Returns information about the server."""return {"info": "This is the MCP GitHub server development for MaximoFN blog post","requested_id": ctx.request_id}# Use: ¿Puedes leer el resource github://repo/facebook/react para obtener información detallada del repositorio?@mcp.resource("github://repo/{owner}/{repo_name}", tags={"public"})async def repository_info(owner: str, repo_name: str, ctx: Context) -> dict:"""Returns detailed information about a GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')ctx: The context of the requestReturns:dict: Repository information including name, description, stats, etc."""api_url = f"https://api.github.com/repos/{owner}/{repo_name}"ctx.info(f"Fetching repository information from {api_url}...")async with httpx.AsyncClient() as client:try:response = await client.get(api_url, headers=create_github_headers())response.raise_for_status()repo_data = response.json()# Extract relevant repository informationrepo_info = {"name": repo_data.get("name"),"full_name": repo_data.get("full_name"),"description": repo_data.get("description"),"owner": {"login": repo_data.get("owner", {}).get("login"),"type": repo_data.get("owner", {}).get("type")},"html_url": repo_data.get("html_url"),"clone_url": repo_data.get("clone_url"),"ssh_url": repo_data.get("ssh_url"),"language": repo_data.get("language"),"size": repo_data.get("size"), # Size in KB"stargazers_count": repo_data.get("stargazers_count"),"watchers_count": repo_data.get("watchers_count"),"forks_count": repo_data.get("forks_count"),"open_issues_count": repo_data.get("open_issues_count"),"default_branch": repo_data.get("default_branch"),"created_at": repo_data.get("created_at"),"updated_at": repo_data.get("updated_at"),"pushed_at": repo_data.get("pushed_at"),"is_private": repo_data.get("private"),"is_fork": repo_data.get("fork"),"is_archived": repo_data.get("archived"),"has_issues": repo_data.get("has_issues"),"has_projects": repo_data.get("has_projects"),"has_wiki": repo_data.get("has_wiki"),"license": repo_data.get("license", {}).get("name") if repo_data.get("license") else None,"topics": repo_data.get("topics", [])}ctx.info(f"Successfully retrieved information for repository {owner}/{repo_name}")return repo_infoexcept httpx.HTTPStatusError as e:error_message = e.response.json().get("message", "No additional message from API.")if e.response.status_code == 404:error_message = f"Repository {owner}/{repo_name} not found or is private."elif e.response.status_code == 403 and GITHUB_TOKEN:error_message += " (Rate limit with token or token lacks permissions?)"elif e.response.status_code == 403 and not GITHUB_TOKEN:error_message += " (Rate limit without token. Consider creating a .env file with GITHUB_TOKEN.)"ctx.error(f"GitHub API error: {e.response.status_code}. {error_message}")return {"error": f"GitHub API error: {e.response.status_code}","message": error_message,"repository": f"{owner}/{repo_name}"}except Exception as e:ctx.error(f"An unexpected error occurred: {str(e)}")return {"error": f"An unexpected error occurred: {str(e)}","repository": f"{owner}/{repo_name}"}@mcp.prompt(name="generate_issues_prompt",description="Generates a structured prompt for asking about GitHub repository issues. Use this when users want to formulate questions about repository issues, or need help creating prompts for issue analysis.",tags={"public"})def generate_issues_prompt(owner: str, repo_name: str) -> str:"""Generates a structured prompt for asking about GitHub repository issues.This prompt template helps users formulate clear questions about repository issuesand can be used as a starting point for issue analysis or research.Args:owner: Repository owner (e.g., 'huggingface', 'microsoft')repo_name: Repository name (e.g., 'transformers', 'vscode')Returns:A formatted prompt asking about repository issues"""return f"""Please provide information about the open issues in the repository {owner}/{repo_name}.I'm interested in:- Current open issues and their status- Recent issue trends and patterns- Common issue categories or topics- Any critical or high-priority issuesRepository: {owner}/{repo_name}"""@sub_mcp.tool(tags={"public"})def hello_world() -> str:"""Returns a simple greeting."""return "Hello, world!"mcp.mount("sub_mcp", sub_mcp)if __name__ == "__main__":print("DEBUG: Starting FastMCP GitHub server...")print(f"DEBUG: Server name: {mcp.name}")# Initialize and run the servermcp.run(transport="stdio")Copied
Overwriting gitHub_MCP_server/github_server.py
MCP Client
We modified our client so it can use the prompt we created on our server.
InputPython%%writefile client_MCP/client.pyimport sysimport asynciofrom contextlib import AsyncExitStackfrom anthropic import Anthropicfrom dotenv import load_dotenvfrom fastmcp import Client# Load environment variables from .env fileload_dotenv()class FastMCPClient:"""FastMCP client that integrates with Claude to process user queriesand use tools and resources exposed by a FastMCP server."""def __init__(self):"""Initialize the FastMCP client with Anthropic and resource management."""self.exit_stack = AsyncExitStack()self.anthropic = Anthropic()self.client = Noneasync def connect_to_server(self, server_script_path: str):"""Connect to the specified FastMCP server.Args:server_script_path: Path to the server script (Python)"""print(f"🔗 Connecting to FastMCP server: {server_script_path}")# Determine the server type based on the extensionif not server_script_path.endswith('.py'):raise ValueError(f"Unsupported server type. Use .py files. Received: {server_script_path}")# Create FastMCP clientself.client = Client(server_script_path)# Note: FastMCP Client automatically infers transport from .py filesprint("✅ Client created successfully")async def list_available_tools(self):"""List available tools in the FastMCP server."""try:# Get list of tools from the server using FastMCP contextasync with self.client as client:tools = await client.list_tools()if tools:print(f" 🛠️ Available tools ({len(tools)}):")print("=" * 50)for tool in tools:print(f"📋 {tool.name}")if tool.description:print(f" Description: {tool.description}")# Show parameters if availableif hasattr(tool, 'inputSchema') and tool.inputSchema:if 'properties' in tool.inputSchema:params = list(tool.inputSchema['properties'].keys())print(f" Parameters: {', '.join(params)}")print()else:print("⚠️ No tools found in the server")except Exception as e:print(f"❌ Error listing tools: {str(e)}")async def list_available_resources(self):"""List available resources in the FastMCP server."""try:# Get list of resources from the server using FastMCP contextasync with self.client as client:resources = await client.list_resources()if resources:print(f" 📚 Available resources ({len(resources)}):")print("=" * 50)for resource in resources:print(f"📄 {resource.uri}")if resource.name:print(f" Name: {resource.name}")if resource.description:print(f" Description: {resource.description}")if resource.mimeType:print(f" MIME Type: {resource.mimeType}")print()else:print("⚠️ No resources found in the server")except Exception as e:print(f"❌ Error listing resources: {str(e)}")async def list_available_prompts(self):"""List available prompts in the FastMCP server."""try:# Get list of prompts from the server using FastMCP contextasync with self.client as client:prompts = await client.list_prompts()if prompts:print(f" 💭 Available prompts ({len(prompts)}):")print("=" * 50)for prompt in prompts:print(f"🎯 {prompt.name}")if prompt.description:print(f" Description: {prompt.description}")# Show parameters if availableif hasattr(prompt, 'arguments') and prompt.arguments:params = []for arg in prompt.arguments:param_info = f"{arg.name}: {arg.description or 'No description'}"if arg.required:param_info += " (required)"params.append(param_info)print(f" Parameters: {', '.join(params)}")print()else:print("⚠️ No prompts found in the server")except Exception as e:print(f"❌ Error listing prompts: {str(e)}")async def read_resource(self, resource_uri: str):"""Read a specific resource from the server.Args:resource_uri: URI of the resource to readReturns:str: Resource content"""try:async with self.client as client:result = await client.read_resource(resource_uri)return resultexcept Exception as e:print(f"❌ Error reading resource {resource_uri}: {str(e)}")return Noneasync def get_prompt(self, prompt_name: str, prompt_args: dict = None):"""Get/call a specific prompt from the server.Args:prompt_name: Name of the prompt to callprompt_args: Arguments for the prompt (if any)Returns:str: Generated prompt content"""try:async with self.client as client:if prompt_args:result = await client.get_prompt(prompt_name, prompt_args)else:result = await client.get_prompt(prompt_name)# Extract the prompt text from the responseif hasattr(result, 'messages') and result.messages:# FastMCP returns prompts as message objectsreturn ' '.join([msg.content.text for msg in result.messages if hasattr(msg.content, 'text')])elif hasattr(result, 'content'):return str(result.content)else:return str(result)except Exception as e:print(f"❌ Error getting prompt {prompt_name}: {str(e)}")return Noneasync def process_query(self, query: str) -> str:"""Process a user query, interacting with Claude and FastMCP tools and resources.Args:query: User queryReturns:str: Final processed response"""try:# Use FastMCP context for all operationsasync with self.client as client:# Get available tools, resources, and promptstools_list = await client.list_tools()resources_list = await client.list_resources()prompts_list = await client.list_prompts()# Prepare tools for Claude in correct formatclaude_tools = []for tool in tools_list:claude_tool = {"name": tool.name,"description": tool.description or f"Tool {tool.name}","input_schema": tool.inputSchema or {"type": "object", "properties": {}}}claude_tools.append(claude_tool)# Add a special tool for reading resources (including template resources)resource_description = "Read a resource from the MCP server. "if resources_list:# Convert URIs to strings to avoid AnyUrl object issuesresource_uris = [str(r.uri) for r in resources_list]resource_description += f"Available static resources: {', '.join(resource_uris)}. "resource_description += "Also supports template resources like github://repo/owner/repo_name for GitHub repository information."claude_tools.append({"name": "read_mcp_resource","description": resource_description,"input_schema": {"type": "object","properties": {"resource_uri": {"type": "string","description": "URI of the resource to read. Can be static (like resource://server_info) or template-based (like github://repo/facebook/react)"}},"required": ["resource_uri"]}})# Add a special tool for using promptsprompt_description = "Generate specialized prompts from the MCP server. Use this when users want to: "prompt_description += "- Create well-structured questions about repositories "prompt_description += "- Get help formulating prompts for specific tasks "prompt_description += "- Generate template questions for analysis "if prompts_list:prompt_names = [p.name for p in prompts_list]prompt_description += f" Available prompts: {', '.join(prompt_names)} "prompt_description += "- generate_issues_prompt: Creates structured questions about GitHub repository issues"prompt_description += " IMPORTANT: Use prompts when users explicitly ask for help creating questions or prompts, or when they want to formulate better questions about repositories."claude_tools.append({"name": "use_mcp_prompt","description": prompt_description,"input_schema": {"type": "object","properties": {"prompt_name": {"type": "string","description": "Name of the prompt to use. Available: 'generate_issues_prompt'"},"prompt_args": {"type": "object","description": "Arguments for the prompt. For generate_issues_prompt: {'owner': 'repo-owner', 'repo_name': 'repo-name'}","properties": {"owner": {"type": "string","description": "Repository owner (e.g., 'huggingface', 'microsoft')"},"repo_name": {"type": "string","description": "Repository name (e.g., 'transformers', 'vscode')"}}}},"required": ["prompt_name"]}})# Create initial message for Claudemessages = [{"role": "user","content": query}]# First call to Clauderesponse = self.anthropic.messages.create(model="claude-3-5-sonnet-20241022",max_tokens=6000,messages=messages,tools=claude_tools if claude_tools else None)# Process Claude's responseresponse_text = ""for content_block in response.content:if content_block.type == "text":response_text += content_block.textelif content_block.type == "tool_use":# Claude wants to use a tooltool_name = content_block.nametool_args = content_block.inputtool_call_id = content_block.idprint(f"🔧 Claude wants to use: {tool_name}")print(f"📝 Arguments: {tool_args}")try:if tool_name == "read_mcp_resource":# Handle resource readingresource_uri = tool_args.get("resource_uri")if resource_uri:tool_result = await client.read_resource(resource_uri)print(f"📖 Resource read successfully: {resource_uri}")# Better handling of resource resultif hasattr(tool_result, 'content'):# If it's a resource response object, extract contentif hasattr(tool_result.content, 'text'):result_content = tool_result.content.textelse:result_content = str(tool_result.content)else:# If it's already a string or simple objectresult_content = str(tool_result)else:tool_result = "Error: No resource URI provided"result_content = tool_resultelif tool_name == "use_mcp_prompt":# Handle prompt usageprompt_name = tool_args.get("prompt_name")prompt_args = tool_args.get("prompt_args", {})if prompt_name:tool_result = await self.get_prompt(prompt_name, prompt_args)print(f"💭 Prompt '{prompt_name}' generated successfully")result_content = str(tool_result) if tool_result else "Error generating prompt"else:tool_result = "Error: No prompt name provided"result_content = tool_resultelse:# Execute regular tool on the FastMCP servertool_result = await client.call_tool(tool_name, tool_args)print(f"✅ Tool executed successfully")result_content = str(tool_result)# Add tool result to the conversationmessages.append({"role": "assistant","content": response.content})# Format result for Claudeif tool_result:messages.append({"role": "user","content": [{"type": "tool_result","tool_use_id": tool_call_id,"content": f"Tool result: {result_content}"}]})else:messages.append({"role": "user","content": [{"type": "tool_result","tool_use_id": tool_call_id,"content": "Tool executed without response content"}]})# Second call to Claude with the tool resultfinal_response = self.anthropic.messages.create(model="claude-3-5-sonnet-20241022",max_tokens=6000,messages=messages,tools=claude_tools if claude_tools else None)# Extract text from the final responsefor final_content in final_response.content:if final_content.type == "text":response_text += final_content.textexcept Exception as e:error_msg = f"❌ Error executing {tool_name}: {str(e)}"print(error_msg)response_text += f" {error_msg}"return response_textexcept Exception as e:error_msg = f"❌ Error processing query: {str(e)}"print(error_msg)return error_msgasync def chat_loop(self):"""Main chat loop with user interaction."""print(" 🤖 FastMCP client started. Write 'quit', 'q', 'exit', 'salir' to exit.")print("💬 You can ask questions about GitHub repositories!")print("📚 The client can use tools, resources, and prompts from the FastMCP server")print()print("💭 PROMPT Examples:")print(" • 'Generate a prompt for asking about issues in facebook/react'")print(" • 'Help me create a good question about microsoft/vscode issues'")print(" • 'I need a structured prompt for analyzing tensorflow/tensorflow'")print()print("🔧 DIRECT Examples:")print(" • 'Show me the issues in huggingface/transformers'")print(" • 'Get repository info for github://repo/google/chrome'")print("-" * 60)while True:try:# Request user inputuser_input = input(" 👤 You: ").strip()if user_input.lower() in ['quit', 'q', 'exit', 'salir']:print("👋 Bye!")breakif not user_input:continueprint(" 🤔 Claude is thinking...")# Process queryresponse = await self.process_query(user_input)# Show responseprint(f" 🤖 Claude: {response}")except KeyboardInterrupt:print(" 👋 Disconnecting...")breakexcept Exception as e:print(f" ❌ Error in chat: {str(e)}")continueasync def cleanup(self):"""Clean up resources and close connections."""print("🧹 Cleaning up resources...")# FastMCP Client cleanup is handled automatically by context managerawait self.exit_stack.aclose()print("✅ Resources released")async def main():"""Main function that initializes and runs the FastMCP client."""# Verify command line argumentsif len(sys.argv) != 2:print("❌ Usage: python client.py <path_to_fastmcp_server>")print("📝 Example: python client.py ../MCP_github/github_server.py")sys.exit(1)server_script_path = sys.argv[1]# Create and run clientclient = FastMCPClient()try:# Connect to the serverawait client.connect_to_server(server_script_path)# List available tools, resources, and prompts after connectionawait client.list_available_tools()await client.list_available_resources()await client.list_available_prompts()# Start chat loopawait client.chat_loop()except Exception as e:print(f"❌ Fatal error: {str(e)}")finally:# Ensure resources are cleaned upawait client.cleanup()if __name__ == "__main__":# Entry point of the scriptasyncio.run(main())Copied
Overwriting client_MCP/client.py
We have created the list_available_prompts and get_prompt functions to list the available prompts and retrieve a specific prompt.
prompt Test
We run the client
InputPython!cd client_MCP && source .venv/bin/activate && python client.py ../gitHub_MCP_server/github_server.pyCopied
🔗 Connecting to FastMCP server: ../gitHub_MCP_server/github_server.py✅ Client created successfully🛠️ Available tools (2):==================================================📋 sub_mcp_hello_worldDescription: Returns a simple greeting.Parameters:📋 list_repository_issuesDescription: Lists open issues for a given GitHub repository.Args:owner: The owner of the repository (e.g., 'modelcontextprotocol')repo_name: The name of the repository (e.g., 'python-sdk')ctx: The context of the requestuser_id: The user ID (automatically injected by the server)Returns:list[dict]: A list of dictionaries, each containing information about an issueParameters: owner, repo_name📚 Available resources (1):==================================================📄 resource://server_info...💭 Prompt 'generate_issues_prompt' generated successfully🤖 Claude: I'll help you generate a structured prompt for viewing issues from the Hugging Face Transformers repository using the `use_mcp_prompt` function with the `generate_issues_prompt` prompt type. I have all the required information from your request:- owner: `huggingface'- repo_name: 'transformers'I've generated a structured prompt that you can use to analyze issues in the Hugging Face Transformers repository. This prompt is designed to help you get comprehensive information about the repository's issues, including their current status, trends, categories, and priorities.Would you like me to actually fetch the current issues from the repository using this prompt? If so, I can use the `list_repository_issues` function to get that information for you.👤 You: q👋 Bye!🧹 Cleaning up resources...✅ Resources released
We see that it gives us a list of the available prompts.
💭 Available prompts (1):
==================================================
🎯 generate_issues_prompt
Description: Generates a structured prompt for asking about GitHub repository issues. Use this when users want to formulate questions about repository issues, or need help creating prompts for issue analysis.
Parameters: owner: No description (required), repo_name: No description (required)
🤖 FastMCP client started. Write 'quit', 'q', 'exit', 'salir' to exit.
💬 Puedes hacer preguntas sobre repositorios de GitHub!
📚 The client can use tools, resources, and prompts from the FastMCP server
💭 PROMPT Examples:
• 'Genera un prompt para preguntar sobre problemas en facebook/react'
• 'Help me create a good question about microsoft/vscode issues'
• 'Necesito un prompt estructurado para analizar tensorflow/tensorflow'
🔧 DIRECT Examples:
• 'Show me the issues in huggingface/transformers'
• 'Obtener información del repositorio para github://repo/google/chrome'And when we ask it for a prompt, it gives us the generated prompt.
👤 Tú: ¿Puedes crear un prompt para ver issues del repositorio de Hugging Face Transformers?
🤔 Claude is thinking...
🔧 Claude wants to use: use_mcp_prompt
📝 Arguments: {'prompt_name': 'generate_issues_prompt', 'prompt_args': {'owner': 'huggingface', 'repo_name': 'transformers'}}
💭 Prompt 'generate_issues_prompt' generated successfully
🤖 Claude: Te ayudaré a generar un prompt estructurado para ver issues del repositorio de Hugging Face Transformers usando la función `use_mcp_prompt` con el tipo de prompt `generate_issues_prompt`. Tengo toda la información requerida de tu solicitud:
- owner: "huggingface"
- repo_name: "transformers"I've generated a structured prompt that you can use to analyze issues in the Hugging Face Transformers repository. This prompt is designed to help you get comprehensive information about the repository's issues, including their current status, trends, categories, and priorities.
Would you like me to actually fetch the current issues from the repository using this prompt? If so, I can use the `list_repository_issues` function to get that information for you.---
➡️ **Continue in Part 4: HTTP, authentication and MCP client**.