Molecular similarity search

From InternScience/DrClaw · View interactive version

Molecular similarity search is an AI agent skill from the GitHub repository InternScience/DrClaw. Search for similar molecules using Tanimoto similarity with Morgan fingerprints to identify structurally related compounds.

How to use this skill

Skill file

Molecular Similarity Search

Usage

1. MCP Server Definition

import asyncio
import json
from mcp.client.streamable_http import streamablehttp_client
from mcp import ClientSession

class DrugSDAClient:
    """DrugSDA-Tool MCP Client"""

    def __init__(self, server_url: str, api_key: str):
        self.server_url = server_url
        self.api_key = api_key
        self.session = None

    async def connect(self):
        """Establish connection and initialize session"""
        print(f"server url: {self.server_url}")
        try:
            self.transport = streamablehttp_client(
                url=self.server_url,
                headers={"SCP-HUB-API-KEY": self.api_key}
            )
            self.read, self.write, self.get_session_id = await self.transport.__aenter__()

            self.session_ctx = ClientSession(self.read, self.write)
            self.session = await self.session_ctx.__aenter__()

            await self.session.initialize()
            session_id = self.get_session_id()

            print(f"✓ connect success")
            return True

        except Exception as e:
            print(f"✗ connect failure: {e}")
            return False

    async def disconnect(self):
        """Disconnect from server"""
        try:
            if self.session:
                await self.session_ctx.__aexit__(None, None, None)
            if hasattr(self, 'transport'):
                await self.transport.__aexit__(None, None, None)
            print("✓ already disconnect")
        except Exception as e:
            print(f"✗ disconnect error: {e}")

    def parse_result(self, result):
        """Parse MCP tool call result"""
        try:
            if hasattr(result, 'content') and result.content:
                content = result.content[0]
                if hasattr(content, 'text'):
                    return json.loads(content.text)
            return str(result)
        except Exception as e:
            return {"error": f"parse error: {e}", "raw": str(result)}

2. Molecular Similarity Search Workflow

This workflow searches for similar molecules using Tanimoto similarity calculated from Morgan fingerprints.

Workflow Steps:

  1. Define Target Molecule - Specify the query SMILES
  2. Define Candidate Molecules - Provide list of candidate SMILES
  3. Calculate Similarity - Compute Tanimoto scores for all candidates
  4. Rank Results - Sort by similarity score to find most similar molecules

Implementation:

## Initialize client
client = DrugSDAClient(
    "https://scp.intern-ai.org.cn/api/v1/mcp/2/DrugSDA-Tool",
    "<your-api-key>"
)

if not await client.connect():
    print("connection failed")
    exit()

## Input: Target molecule and candidate library
target = "CCO"  # Ethanol
candidates = [
    "CCCO",      # Propanol
    "CCCCO",     # Butanol
    "CC(C)O",    # Isopropanol
    "CCC(C)O",   # sec-Butanol
    "C1CC1",     # Cyclopropane
    "CC=O",      # Acetaldehyde
    "CCCOO"      # Propanoic acid
]

## Execute similarity calculation
result = await client.session.call_tool(
    "calculate_smiles_similarity",
    arguments={
        "target_smiles": target,
        "candidate_smiles_list": candidates
    }
)

result_data = client.parse_result(result)
similarities = result_data['similarities']

## Sort and display top 3 most similar molecules
top3_smiles = sorted(similarities, key=lambda x: x['score'], reverse=True)[:3]

print(f"Target molecule: {target}\n")
print("Top 3 most similar molecules:")
for i, item in enumerate(top3_smiles, 1):
    print(f"{i}. {item['smiles']} - Tanimoto score: {item['score']:.4f}")

await client.disconnect()

Tool Descriptions

DrugSDA-Tool Server:

Input/Output

Input:

Output:

Similarity Interpretation

Use Cases

Performance Notes

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.