Skip to main content
Glama

read_course_file

Read-only

Retrieve a file's content as base64 from a Canvas course, using file IDs from list_course_files or list_module_items. Specify course identifier and optional max size (default 25 MB) to control memory usage.

Instructions

Read a file from a Canvas course and return its content as base64.

    Unlike download_course_file which saves to the server's local filesystem,
    this tool returns the file content directly in the response. This is useful
    when the MCP server runs on a different machine than the client.

    Use list_course_files or list_module_items to find file IDs.

    Args:
        course_identifier: Course code or Canvas ID
        file_id: Canvas file ID
        max_size_mb: Maximum file size in MB to read (default: 25). Clamped server-side to
            READ_FILE_MAX_SIZE_MB (default 100). Files larger than the effective limit are
            rejected to avoid excessive memory usage.
    

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
course_identifierYes
file_idYes
max_size_mbNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The actual handler function for the 'read_course_file' tool. Gets file metadata via Canvas API, downloads the file content into memory (streaming with size checks), encodes it as base64, and returns it as a formatted string.
    @mcp.tool(annotations=ToolAnnotations(readOnlyHint=True))
    @validate_params
    async def read_course_file(
        course_identifier: str | int,
        file_id: str | int,
        max_size_mb: float = 25.0,
    ) -> str:
        """Read a file from a Canvas course and return its content as base64.
    
        Unlike download_course_file which saves to the server's local filesystem,
        this tool returns the file content directly in the response. This is useful
        when the MCP server runs on a different machine than the client.
    
        Use list_course_files or list_module_items to find file IDs.
    
        Args:
            course_identifier: Course code or Canvas ID
            file_id: Canvas file ID
            max_size_mb: Maximum file size in MB to read (default: 25). Clamped server-side to
                READ_FILE_MAX_SIZE_MB (default 100). Files larger than the effective limit are
                rejected to avoid excessive memory usage.
        """
        if max_size_mb <= 0:
            return (
                f"Error: max_size_mb must be positive (got {max_size_mb}). "
                f"Pass a value like 25 for a 25 MB limit."
            )
    
        server_max_mb = get_config().read_file_max_size_mb
        effective_max_mb = min(float(max_size_mb), server_max_mb)
        max_size_bytes = int(effective_max_mb * 1024 * 1024)
    
        course_id = await get_course_id(course_identifier)
    
        # Get file metadata from Canvas API
        file_info = await make_canvas_request(
            "get",
            f"/courses/{course_id}/files/{file_id}"
        )
    
        if isinstance(file_info, dict) and "error" in file_info:
            return f"Error getting file info: {file_info['error']}"
    
        raw_filename = file_info.get("display_name") or file_info.get("filename", f"file_{file_id}")
        filename = sanitize_filename(raw_filename)
        download_url = file_info.get("url")
        content_type = file_info.get("content-type", "unknown")
        reported_size = file_info.get("size", 0)
    
        if not download_url:
            return "Error: No download URL available for this file. Check permissions."
    
        # Check reported file size before downloading
        if reported_size and reported_size > max_size_bytes:
            return (
                f"Error: File '{filename}' is {format_file_size(reported_size)}, "
                f"which exceeds the {effective_max_mb} MB limit. "
                f"Use download_course_file instead for large files."
            )
    
        # Download the file content into memory
        client = _get_http_client()
        try:
            buffer = bytearray()
            async with client.stream("GET", download_url, follow_redirects=True) as response:
                response.raise_for_status()
    
                async for chunk in response.aiter_bytes(chunk_size=8192):
                    if len(buffer) + len(chunk) > max_size_bytes:
                        return (
                            f"Error: File '{filename}' exceeds the {effective_max_mb} MB limit "
                            f"during download. Use download_course_file instead for large files."
                        )
                    buffer.extend(chunk)
    
            base64_content = base64.b64encode(buffer).decode("ascii")
    
            size_str = format_file_size(len(buffer))
            course_display = await get_course_code(course_id) or course_identifier
    
            result = f"Read: {filename}\n"
            result += f"  Size: {size_str}\n"
            result += f"  Type: {content_type}\n"
            result += f"  Course: {course_display}\n"
            result += "  Encoding: base64\n"
            result += f"  Content:\n{base64_content}\n"
            return result
    
        except Exception as e:
            return f"Error reading file: {str(e)}"
  • Docstring/parameter schema defining the inputs: course_identifier, file_id, max_size_mb (default 25.0, clamped server-side to READ_FILE_MAX_SIZE_MB).
    """Read a file from a Canvas course and return its content as base64.
    
    Unlike download_course_file which saves to the server's local filesystem,
    this tool returns the file content directly in the response. This is useful
    when the MCP server runs on a different machine than the client.
    
    Use list_course_files or list_module_items to find file IDs.
    
    Args:
        course_identifier: Course code or Canvas ID
        file_id: Canvas file ID
        max_size_mb: Maximum file size in MB to read (default: 25). Clamped server-side to
            READ_FILE_MAX_SIZE_MB (default 100). Files larger than the effective limit are
            rejected to avoid excessive memory usage.
  • The registration function that wraps the tool. The @mcp.tool decorator on read_course_file (line 111) registers it as an MCP tool within register_shared_file_tools.
    def register_shared_file_tools(mcp: FastMCP):
        """Register file tools accessible to both students and educators."""
  • The call to register_shared_file_tools in the main server setup, which registers read_course_file along with other shared file tools.
    register_shared_discussion_tools(mcp)
    register_shared_module_tools(mcp)
    register_shared_file_tools(mcp)
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Adds behavioral details beyond annotations: returns base64 content, clamps max_size_mb server-side, rejects files larger than effective limit to avoid memory issues. No contradiction with readOnlyHint annotation.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Well-structured: starts with purpose, contrasts with sibling, provides lookup hints, then details parameters. Efficient despite being somewhat lengthy; no unnecessary sentences.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Covers usage context, alternative, behavioral constraints, and parameter details. Output schema exists so return value details are not needed. Minor gap: no mention of error cases, but acceptable for a read tool.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Input schema has 0% description coverage, but description explains each parameter (course_identifier, file_id, max_size_mb) with default and clamping behavior, adding value over the schema's titles.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Description clearly states it reads a file and returns base64 content, and distinguishes itself from sibling download_course_file by noting it returns content directly rather than saving to filesystem.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Explicitly guides to use list_course_files or list_module_items to find file IDs, and explains when this tool is preferable (when MCP server runs on different machine). Does not explicitly state when not to use, but provides clear alternative.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/vishalsachdev/canvas-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server