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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#!/usr/bin/env python3
"""

Gradio MCP Server for Congressional Bioguide profiles.

Provides search and analysis capabilities via Gradio interface.

"""

import gradio as gr
import sqlite3
import json
import os
import warnings
from typing import List, Dict, Any
import numpy as np
from sentence_transformers import SentenceTransformer
import faiss
import pickle
from pathlib import Path

# Suppress warnings
warnings.filterwarnings('ignore')
os.environ['TOKENIZERS_PARALLELISM'] = 'false'

# Initialize global resources
SCRIPT_DIR = Path(__file__).parent.absolute()
DB_PATH = str(SCRIPT_DIR / "congress.db")
FAISS_INDEX_PATH = str(SCRIPT_DIR / "congress_faiss.index")
BIO_IDS_PATH = str(SCRIPT_DIR / "congress_bio_ids.pkl")

# Global state
model = None
faiss_index = None
bio_id_mapping = None


def initialize_search_index():
    """Initialize the semantic search components."""
    global model, faiss_index, bio_id_mapping

    try:
        if Path(FAISS_INDEX_PATH).exists() and Path(BIO_IDS_PATH).exists():
            print(f"Loading FAISS index from: {FAISS_INDEX_PATH}")
            model = SentenceTransformer('all-MiniLM-L6-v2')
            faiss_index = faiss.read_index(FAISS_INDEX_PATH)
            with open(BIO_IDS_PATH, "rb") as f:
                bio_id_mapping = pickle.load(f)
            print(f"βœ“ Loaded {faiss_index.ntotal} embeddings")
            return True
        else:
            print(f"FAISS index not found. Semantic search will be unavailable.")
            return False
    except Exception as e:
        print(f"Error loading search index: {e}")
        return False


def get_db_connection():
    """Get a database connection."""
    return sqlite3.connect(DB_PATH)


def execute_query(query: str, params: tuple = ()) -> List[Dict[str, Any]]:
    """Execute a SQL query and return results as list of dicts."""
    conn = get_db_connection()
    conn.row_factory = sqlite3.Row
    cursor = conn.cursor()
    cursor.execute(query, params)
    results = [dict(row) for row in cursor.fetchall()]
    conn.close()
    return results


# Initialize search index on startup
print("Initializing Congressional Bioguide MCP Server...")
initialize_search_index()


# MCP Tool Functions with decorators
@gr.mcp.tool()
def search_by_name(family_name: str = "", given_name: str = "", limit: int = 10) -> str:
    """

    Search for Congressional members by name.



    Args:

        family_name: Last name to search for (partial match)

        given_name: First name to search for (partial match)

        limit: Maximum number of results to return (default: 10)



    Returns:

        JSON string with search results including bio_id, name, birth/death dates, party, state

    """
    try:
        conditions = []
        params = []

        if family_name:
            conditions.append("LOWER(m.unaccented_family_name) LIKE LOWER(?)")
            params.append(f"%{family_name}%")
        if given_name:
            conditions.append("LOWER(m.unaccented_given_name) LIKE LOWER(?)")
            params.append(f"%{given_name}%")

        if not conditions:
            return json.dumps({"error": "Please provide at least family_name or given_name"})

        query = f"""

        SELECT DISTINCT m.bio_id, m.given_name, m.middle_name, m.family_name,

               m.birth_date, m.death_date,

               j.party, j.region_code, j.job_name, j.congress_number

        FROM members m

        LEFT JOIN job_positions j ON m.bio_id = j.bio_id

        WHERE {' AND '.join(conditions)}

        ORDER BY m.family_name, m.given_name

        LIMIT ?

        """
        params.append(limit)
        results = execute_query(query, tuple(params))

        return json.dumps({"count": len(results), "results": results}, indent=2)

    except Exception as e:
        return json.dumps({"error": str(e)})


@gr.mcp.tool()
def search_by_party(party: str, congress_number: int = None) -> str:
    """

    Search for Congressional members by political party.



    Args:

        party: Party name (e.g., 'Republican', 'Democrat', 'Whig')

        congress_number: Optional Congress number to filter by (e.g., 117)



    Returns:

        JSON string with members from the specified party

    """
    try:
        if congress_number:
            query = """

            SELECT DISTINCT m.bio_id, m.given_name, m.family_name, m.birth_date, m.death_date,

                   j.party, j.region_code, j.job_name, j.congress_number

            FROM members m

            JOIN job_positions j ON m.bio_id = j.bio_id

            WHERE j.party = ? AND j.congress_number = ?

            ORDER BY m.family_name, m.given_name

            LIMIT 100

            """
            results = execute_query(query, (party, congress_number))
        else:
            query = """

            SELECT DISTINCT m.bio_id, m.given_name, m.family_name, m.birth_date, m.death_date,

                   j.party, j.region_code, j.job_name, j.congress_number

            FROM members m

            JOIN job_positions j ON m.bio_id = j.bio_id

            WHERE j.party = ?

            ORDER BY m.family_name, m.given_name

            LIMIT 100

            """
            results = execute_query(query, (party,))

        return json.dumps({"count": len(results), "party": party, "results": results}, indent=2)

    except Exception as e:
        return json.dumps({"error": str(e)})


@gr.mcp.tool()
def search_by_state(state_code: str, congress_number: int = None) -> str:
    """

    Search for Congressional members by state.



    Args:

        state_code: Two-letter state code (e.g., 'CA', 'NY', 'TX')

        congress_number: Optional Congress number to filter by



    Returns:

        JSON string with members from the specified state

    """
    try:
        state_code = state_code.upper()

        if congress_number:
            query = """

            SELECT DISTINCT m.bio_id, m.given_name, m.family_name, m.birth_date, m.death_date,

                   j.party, j.region_code, j.job_name, j.congress_number

            FROM members m

            JOIN job_positions j ON m.bio_id = j.bio_id

            WHERE j.region_code = ? AND j.congress_number = ?

            ORDER BY m.family_name, m.given_name

            LIMIT 100

            """
            results = execute_query(query, (state_code, congress_number))
        else:
            query = """

            SELECT DISTINCT m.bio_id, m.given_name, m.family_name, m.birth_date, m.death_date,

                   j.party, j.region_code, j.job_name, j.congress_number

            FROM members m

            JOIN job_positions j ON m.bio_id = j.bio_id

            WHERE j.region_code = ?

            ORDER BY m.family_name, m.given_name

            LIMIT 100

            """
            results = execute_query(query, (state_code,))

        return json.dumps({"count": len(results), "state": state_code, "results": results}, indent=2)

    except Exception as e:
        return json.dumps({"error": str(e)})


@gr.mcp.tool()
def semantic_search_biography(query: str, top_k: int = 5) -> str:
    """

    Perform AI-powered semantic search on member biographies using natural language.



    Args:

        query: Natural language query (e.g., 'lawyers who became judges', 'Civil War veterans')

        top_k: Number of results to return (default: 5, max: 20)



    Returns:

        JSON string with matching members and their similarity scores

    """
    try:
        if not all([model, faiss_index, bio_id_mapping]):
            return json.dumps({"error": "Semantic search is not available. FAISS index not loaded."})

        # Limit top_k
        top_k = min(max(1, top_k), 20)

        # Encode query
        query_embedding = model.encode([query])[0].astype('float32')
        query_embedding = query_embedding.reshape(1, -1)
        faiss.normalize_L2(query_embedding)

        # Search
        scores, indices = faiss_index.search(query_embedding, top_k)

        # Get profiles
        results = []
        for idx, score in zip(indices[0], scores[0]):
            if idx < len(bio_id_mapping):
                bio_id = bio_id_mapping[idx]
                member_query = """

                SELECT m.bio_id, m.given_name, m.middle_name, m.family_name,

                       m.birth_date, m.death_date, m.profile_text,

                       j.party, j.region_code, j.job_name, j.congress_number

                FROM members m

                LEFT JOIN job_positions j ON m.bio_id = j.bio_id

                WHERE m.bio_id = ?

                LIMIT 1

                """
                member_data = execute_query(member_query, (bio_id,))
                if member_data:
                    member = member_data[0]
                    # Truncate profile_text for response
                    if member.get('profile_text'):
                        member['profile_text'] = member['profile_text'][:500] + "..."
                    member['similarity_score'] = float(score)
                    results.append(member)

        return json.dumps({"query": query, "count": len(results), "results": results}, indent=2)

    except Exception as e:
        return json.dumps({"error": str(e)})


@gr.mcp.tool()
def get_member_profile(bio_id: str) -> str:
    """

    Get complete profile for a specific member by their Bioguide ID.



    Args:

        bio_id: Bioguide ID (e.g., 'L000313' for John Lewis, 'W000374')



    Returns:

        JSON string with complete member profile including positions and relationships

    """
    try:
        bio_id = bio_id.upper()

        conn = get_db_connection()
        conn.row_factory = sqlite3.Row
        cursor = conn.cursor()

        cursor.execute("SELECT * FROM members WHERE bio_id = ?", (bio_id,))
        member = cursor.fetchone()

        if not member:
            conn.close()
            return json.dumps({"error": f"No member found with bio_id: {bio_id}"})

        profile = dict(member)

        # Get job positions
        cursor.execute("SELECT * FROM job_positions WHERE bio_id = ? ORDER BY start_date", (bio_id,))
        profile['job_positions'] = [dict(row) for row in cursor.fetchall()]

        # Get relationships
        cursor.execute("SELECT * FROM relationships WHERE bio_id = ?", (bio_id,))
        profile['relationships'] = [dict(row) for row in cursor.fetchall()]

        # Get creative works
        cursor.execute("SELECT * FROM creative_works WHERE bio_id = ?", (bio_id,))
        profile['creative_works'] = [dict(row) for row in cursor.fetchall()]

        conn.close()

        return json.dumps(profile, indent=2)

    except Exception as e:
        return json.dumps({"error": str(e)})


@gr.mcp.tool()
def count_members_by_party(filter_congress: int = None) -> str:
    """

    Count members by political party.



    Args:

        filter_congress: Optional Congress number to filter by (e.g., 117)



    Returns:

        JSON string with member counts grouped by party

    """
    try:
        if filter_congress:
            query = """

            SELECT j.party as party, COUNT(DISTINCT m.bio_id) as count

            FROM members m

            JOIN job_positions j ON m.bio_id = j.bio_id

            WHERE j.congress_number = ?

            GROUP BY j.party

            ORDER BY count DESC

            """
            results = execute_query(query, (filter_congress,))
        else:
            query = """

            SELECT j.party as party, COUNT(DISTINCT m.bio_id) as count

            FROM members m

            JOIN job_positions j ON m.bio_id = j.bio_id

            GROUP BY j.party

            ORDER BY count DESC

            """
            results = execute_query(query)

        total = sum(r['count'] for r in results)
        return json.dumps({"total_members": total, "by_party": results}, indent=2)

    except Exception as e:
        return json.dumps({"error": str(e)})


@gr.mcp.tool()
def count_members_by_state(filter_congress: int = None) -> str:
    """

    Count members by state.



    Args:

        filter_congress: Optional Congress number to filter by



    Returns:

        JSON string with member counts grouped by state

    """
    try:
        if filter_congress:
            query = """

            SELECT j.region_code as state, COUNT(DISTINCT m.bio_id) as count

            FROM members m

            JOIN job_positions j ON m.bio_id = j.bio_id

            WHERE j.congress_number = ?

            GROUP BY j.region_code

            ORDER BY count DESC

            """
            results = execute_query(query, (filter_congress,))
        else:
            query = """

            SELECT j.region_code as state, COUNT(DISTINCT m.bio_id) as count

            FROM members m

            JOIN job_positions j ON m.bio_id = j.bio_id

            GROUP BY j.region_code

            ORDER BY count DESC

            """
            results = execute_query(query)

        total = sum(r['count'] for r in results)
        return json.dumps({"total_members": total, "by_state": results}, indent=2)

    except Exception as e:
        return json.dumps({"error": str(e)})


@gr.mcp.tool()
def execute_sql_query(query: str) -> str:
    """

    Execute a custom SQL SELECT query against the Congressional database (READ-ONLY).



    Args:

        query: SQL SELECT query to execute



    Returns:

        JSON string with query results

    """
    try:
        # Security: only allow SELECT queries
        if not query.strip().upper().startswith("SELECT"):
            return json.dumps({"error": "Only SELECT queries are allowed"})

        results = execute_query(query)
        return json.dumps({"count": len(results), "results": results}, indent=2)

    except Exception as e:
        return json.dumps({"error": str(e)})


@gr.mcp.tool()
def get_database_schema() -> str:
    """

    Get the database schema showing all tables and columns available for querying.



    Returns:

        JSON string with database schema information

    """
    schema_info = {
        "tables": {
            "members": {
                "description": "Main table with member biographical information",
                "columns": [
                    "bio_id (PRIMARY KEY) - Bioguide ID",
                    "family_name - Last name",
                    "given_name - First name",
                    "middle_name - Middle name",
                    "birth_date - Birth date (YYYY-MM-DD)",
                    "death_date - Death date (YYYY-MM-DD)",
                    "profile_text - Full biography text"
                ]
            },
            "job_positions": {
                "description": "Congressional positions held by members",
                "columns": [
                    "bio_id (FOREIGN KEY) - References members",
                    "job_name - Position title (Representative, Senator)",
                    "start_date - Start date of position",
                    "end_date - End date of position",
                    "congress_number - Congress number (e.g., 117)",
                    "party - Party affiliation",
                    "region_code - State/region code (e.g., 'CA', 'NY')"
                ]
            },
            "relationships": {
                "description": "Family relationships between members",
                "columns": ["bio_id", "related_bio_id", "relationship_type"]
            },
            "creative_works": {
                "description": "Publications and creative works by members",
                "columns": ["bio_id", "citation_text"]
            }
        }
    }
    return json.dumps(schema_info, indent=2)


# Create Gradio Interfaces for each tool
demo = gr.TabbedInterface(
    [
        # Search by Name
        gr.Interface(
            fn=search_by_name,
            inputs=[
                gr.Textbox(label="Family Name (Last Name)", placeholder="e.g., Lincoln"),
                gr.Textbox(label="Given Name (First Name)", placeholder="e.g., Abraham"),
                gr.Slider(minimum=1, maximum=50, value=10, step=1, label="Max Results")
            ],
            outputs=gr.JSON(label="Search Results"),
            title="Search by Name",
            description="Search for Congressional members by their first or last name."
        ),

        # Search by Party
        gr.Interface(
            fn=search_by_party,
            inputs=[
                gr.Textbox(label="Party Name", placeholder="e.g., Republican, Democrat, Whig"),
                gr.Number(label="Congress Number (optional)", value=None, precision=0)
            ],
            outputs=gr.JSON(label="Search Results"),
            title="Search by Party",
            description="Find members by political party affiliation."
        ),

        # Search by State
        gr.Interface(
            fn=search_by_state,
            inputs=[
                gr.Textbox(label="State Code", placeholder="e.g., CA, NY, TX"),
                gr.Number(label="Congress Number (optional)", value=None, precision=0)
            ],
            outputs=gr.JSON(label="Search Results"),
            title="Search by State",
            description="Find members by the state they represented."
        ),

        # Semantic Search
        gr.Interface(
            fn=semantic_search_biography,
            inputs=[
                gr.Textbox(label="Search Query", placeholder="e.g., 'lawyers who became judges' or 'Civil War veterans'", lines=3),
                gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Number of Results")
            ],
            outputs=gr.JSON(label="Search Results"),
            title="AI Semantic Search",
            description="Use natural language to search biographies. Find members by career, background, or accomplishments."
        ),

        # Get Member Profile
        gr.Interface(
            fn=get_member_profile,
            inputs=gr.Textbox(label="Bioguide ID", placeholder="e.g., L000313 (John Lewis)"),
            outputs=gr.JSON(label="Member Profile"),
            title="Get Member Profile",
            description="Get complete profile for a specific member using their Bioguide ID."
        ),

        # Count by Party
        gr.Interface(
            fn=count_members_by_party,
            inputs=gr.Number(label="Filter by Congress Number (optional)", value=None, precision=0),
            outputs=gr.JSON(label="Party Counts"),
            title="Count by Party",
            description="Get member counts grouped by political party."
        ),

        # Count by State
        gr.Interface(
            fn=count_members_by_state,
            inputs=gr.Number(label="Filter by Congress Number (optional)", value=None, precision=0),
            outputs=gr.JSON(label="State Counts"),
            title="Count by State",
            description="Get member counts grouped by state."
        ),

        # SQL Query
        gr.Interface(
            fn=execute_sql_query,
            inputs=gr.Textbox(label="SQL Query", placeholder="SELECT * FROM members LIMIT 10", lines=3),
            outputs=gr.JSON(label="Query Results"),
            title="Execute SQL",
            description="Execute custom SQL SELECT queries (read-only)."
        ),

        # Database Schema
        gr.Interface(
            fn=get_database_schema,
            inputs=None,
            outputs=gr.JSON(label="Database Schema"),
            title="Database Schema",
            description="View the database structure and available tables/columns."
        ),
    ],
    tab_names=[
        "Search by Name",
        "Search by Party",
        "Search by State",
        "AI Semantic Search",
        "Member Profile",
        "Count by Party",
        "Count by State",
        "Execute SQL",
        "Database Schema"
    ],
    title="πŸ›οΈ Congressional Bioguide MCP Server",
    theme=gr.themes.Soft()
)

if __name__ == "__main__":
    demo.launch(mcp_server=True)