Skip to main content
Glama

get_magnet_link

Generate a magnet link for a specific torrent using its ID without exposing your YggTorrent passkey. Simplify torrent access through programmatic interaction with the YggTorrent MCP Server.

Instructions

Get the magnet link for a specific torrent.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
torrent_idYes

Implementation Reference

  • MCP tool handler for get_magnet_link. This is the primary execution function for the tool, registered via @mcp.tool() decorator.
    @mcp.tool()
    def get_magnet_link(torrent_id: int) -> str | None:
        """Get the magnet link from YggTorrent for a specific torrent by id."""
        logger.info(f"Getting magnet link for torrent: {torrent_id}")
        magnet_link: str | None = ygg_api.get_magnet_link(torrent_id)
        return magnet_link or "Magnet link not found"
  • Core helper function in YggTorrentApi that implements the magnet link retrieval by downloading the torrent file and generating the magnet URI.
    def get_magnet_link(self, torrent_id: int) -> str | None:
        """
        Get the magnet link for a specific torrent.
    
        Args:
            torrent_id: The ID of the torrent.
    
        Returns:
            The magnet link as a string or None.
        """
        try:
            file_bytes = self._download_torrent_file_bytes(torrent_id)
            if file_bytes and isinstance(file_bytes, bytes):
                return make_magnet_from_torrent_bytes(file_bytes)
        except Exception as e:
            print(f"Failed to generate magnet link: {e}")
        return None
  • Utility function that generates the magnet link from raw torrent file bytes by computing info hash, name, size, and adding trackers.
    def make_magnet_from_torrent_bytes(file_bytes: bytes) -> str:
        metadata = decode(file_bytes)
        subj = metadata[b"info"]  # type: ignore
        hashcontents = encode(subj)  # type: ignore
        digest = sha1(hashcontents).digest()
        b32hash = b32encode(digest).decode()
    
        if b"files" in subj:
            total_length = sum(f[b"length"] for f in subj[b"files"])
        else:
            total_length = subj[b"length"]
    
        tracker = f"http://tracker.p2p-world.net:8080/{YGG_PASSKEY}/announce"
        tracker2 = f"http://connect.maxp2p.org:8080/{YGG_PASSKEY}/announce"
        return (
            "magnet:?xt=urn:btih:"
            + b32hash
            + "&dn="
            + parse.quote(subj[b"name"].decode())
            + "&tr="
            + tracker
            + "&tr="
            + tracker2
            + "&xl="
            + str(total_length)
        )
Install Server

Other Tools

Related 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/philogicae/ygg-torrent-mcp'

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